Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 1 | //===--- 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 Majnemer | e11d373 | 2015-06-08 00:22:46 +0000 | [diff] [blame] | 11 | #include "Tools.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 12 | #include "clang/Basic/CharInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 13 | #include "clang/Basic/Version.h" |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 14 | #include "clang/Driver/Compilation.h" |
| 15 | #include "clang/Driver/Driver.h" |
Rafael Espindola | 7976446 | 2013-03-24 15:06:53 +0000 | [diff] [blame] | 16 | #include "clang/Driver/DriverDiagnostic.h" |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 17 | #include "clang/Driver/Options.h" |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Alp Toker | f1ffc84 | 2014-06-22 04:31:15 +0000 | [diff] [blame] | 19 | #include "llvm/Config/llvm-config.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 20 | #include "llvm/Option/Arg.h" |
| 21 | #include "llvm/Option/ArgList.h" |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
| 24 | #include "llvm/Support/Process.h" |
Reid Kleckner | 6b7156b | 2015-01-23 19:16:25 +0000 | [diff] [blame] | 25 | #include <cstdio> |
| 26 | |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 27 | // Include the necessary headers to interface with the Windows registry and |
| 28 | // environment. |
Alp Toker | f1ffc84 | 2014-06-22 04:31:15 +0000 | [diff] [blame] | 29 | #if defined(LLVM_ON_WIN32) |
Alp Toker | fcce183 | 2014-06-22 03:27:45 +0000 | [diff] [blame] | 30 | #define USE_WIN32 |
| 31 | #endif |
| 32 | |
| 33 | #ifdef USE_WIN32 |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 34 | #define WIN32_LEAN_AND_MEAN |
| 35 | #define NOGDI |
Yaron Keren | 7fc6f1e | 2014-12-04 21:46:50 +0000 | [diff] [blame] | 36 | #ifndef NOMINMAX |
| 37 | #define NOMINMAX |
| 38 | #endif |
Logan Chien | 733e3c6 | 2014-06-24 16:18:10 +0000 | [diff] [blame] | 39 | #include <windows.h> |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | using namespace clang::driver; |
| 43 | using namespace clang::driver::toolchains; |
| 44 | using namespace clang; |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 45 | using namespace llvm::opt; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 46 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 47 | MSVCToolChain::MSVCToolChain(const Driver &D, const llvm::Triple& Triple, |
| 48 | const ArgList &Args) |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 49 | : ToolChain(D, Triple, Args) { |
Zachary Turner | 719f58c | 2014-12-01 23:06:47 +0000 | [diff] [blame] | 50 | getProgramPaths().push_back(getDriver().getInstalledDir()); |
| 51 | if (getDriver().getInstalledDir() != getDriver().Dir) |
| 52 | getProgramPaths().push_back(getDriver().Dir); |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 55 | Tool *MSVCToolChain::buildLinker() const { |
Douglas Katzman | 9535429 | 2015-06-23 20:42:09 +0000 | [diff] [blame] | 56 | return new tools::visualstudio::Linker(*this); |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 59 | Tool *MSVCToolChain::buildAssembler() const { |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 60 | if (getTriple().isOSBinFormatMachO()) |
Douglas Katzman | 9535429 | 2015-06-23 20:42:09 +0000 | [diff] [blame] | 61 | return new tools::darwin::Assembler(*this); |
Alp Toker | c8d4f0f | 2013-11-22 08:27:46 +0000 | [diff] [blame] | 62 | getDriver().Diag(clang::diag::err_no_external_assembler); |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 63 | return nullptr; |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 66 | bool MSVCToolChain::IsIntegratedAssemblerDefault() const { |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 70 | bool MSVCToolChain::IsUnwindTablesDefault() const { |
Reid Kleckner | 6b3a940 | 2014-09-04 18:13:12 +0000 | [diff] [blame] | 71 | // 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 Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 77 | bool MSVCToolChain::isPICDefault() const { |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 78 | return getArch() == llvm::Triple::x86_64; |
| 79 | } |
| 80 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 81 | bool MSVCToolChain::isPIEDefault() const { |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 85 | bool MSVCToolChain::isPICDefaultForced() const { |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 86 | return getArch() == llvm::Triple::x86_64; |
| 87 | } |
| 88 | |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 89 | #ifdef USE_WIN32 |
| 90 | static 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 Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 112 | /// \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 Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 117 | /// 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 Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 120 | static bool getSystemRegistryString(const char *keyPath, const char *valueName, |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 121 | std::string &value, std::string *phValue) { |
Alp Toker | fcce183 | 2014-06-22 03:27:45 +0000 | [diff] [blame] | 122 | #ifndef USE_WIN32 |
| 123 | return false; |
| 124 | #else |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 125 | HKEY hRootKey = HKEY_LOCAL_MACHINE; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 126 | HKEY hKey = NULL; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 127 | long lResult; |
| 128 | bool returnValue = false; |
| 129 | |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 130 | const char *placeHolder = strstr(keyPath, "$VERSION"); |
| 131 | std::string bestName; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 132 | // 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 Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 137 | while ((keyEnd > keyPath) && (*keyEnd != '\\')) |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 138 | keyEnd--; |
| 139 | // Find end of key containing $VERSION. |
| 140 | while (*nextKey && (*nextKey != '\\')) |
| 141 | nextKey++; |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 142 | size_t partialKeyLength = keyEnd - keyPath; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 143 | char partialKey[256]; |
| 144 | if (partialKeyLength > sizeof(partialKey)) |
| 145 | partialKeyLength = sizeof(partialKey); |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 146 | strncpy(partialKey, keyPath, partialKeyLength); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 147 | partialKey[partialKeyLength] = '\0'; |
| 148 | HKEY hTopKey = NULL; |
Hans Wennborg | 935d01d | 2013-10-09 23:41:48 +0000 | [diff] [blame] | 149 | lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY, |
| 150 | &hTopKey); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 151 | if (lResult == ERROR_SUCCESS) { |
| 152 | char keyName[256]; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 153 | 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 Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 158 | while (*sp && !isDigit(*sp)) |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 159 | sp++; |
| 160 | if (!*sp) |
| 161 | continue; |
| 162 | const char *ep = sp + 1; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 163 | while (*ep && (isDigit(*ep) || (*ep == '.'))) |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 164 | ep++; |
| 165 | char numBuf[32]; |
| 166 | strncpy(numBuf, sp, sizeof(numBuf) - 1); |
| 167 | numBuf[sizeof(numBuf) - 1] = '\0'; |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 168 | 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 Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 172 | bestName = keyName; |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 173 | // Append rest of key. |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 174 | bestName.append(nextKey); |
| 175 | lResult = RegOpenKeyEx(hTopKey, bestName.c_str(), 0, |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 176 | KEY_READ | KEY_WOW64_32KEY, &hKey); |
| 177 | if (lResult == ERROR_SUCCESS) { |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 178 | lResult = readFullStringValue(hKey, valueName, value); |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 179 | if (lResult == ERROR_SUCCESS) { |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 180 | bestValue = dvalue; |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 181 | if (phValue) |
| 182 | *phValue = bestName; |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 183 | returnValue = true; |
| 184 | } |
| 185 | RegCloseKey(hKey); |
| 186 | } |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 187 | } |
| 188 | size = sizeof(keyName) - 1; |
| 189 | } |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 190 | RegCloseKey(hTopKey); |
| 191 | } |
| 192 | } else { |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 193 | lResult = |
| 194 | RegOpenKeyEx(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 195 | if (lResult == ERROR_SUCCESS) { |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 196 | lResult = readFullStringValue(hKey, valueName, value); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 197 | if (lResult == ERROR_SUCCESS) |
| 198 | returnValue = true; |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 199 | if (phValue) |
| 200 | phValue->clear(); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 201 | RegCloseKey(hKey); |
| 202 | } |
| 203 | } |
| 204 | return returnValue; |
Alp Toker | fcce183 | 2014-06-22 03:27:45 +0000 | [diff] [blame] | 205 | #endif // USE_WIN32 |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame^] | 208 | // Convert LLVM's ArchType |
| 209 | // to the corresponding name of Windows SDK libraries subfolder |
| 210 | static StringRef getWindowsSDKArch(llvm::Triple::ArchType Arch) { |
| 211 | switch (Arch) { |
| 212 | case llvm::Triple::x86: |
| 213 | return "x86"; |
| 214 | case llvm::Triple::x86_64: |
| 215 | return "x64"; |
| 216 | case llvm::Triple::arm: |
| 217 | return "arm"; |
| 218 | default: |
| 219 | return ""; |
| 220 | } |
| 221 | } |
| 222 | |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 223 | /// \brief Get Windows SDK installation directory. |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 224 | bool MSVCToolChain::getWindowsSDKDir(std::string &path, int &major, |
| 225 | int &minor) const { |
| 226 | std::string sdkVersion; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 227 | // Try the Windows registry. |
| 228 | bool hasSDKDir = getSystemRegistryString( |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 229 | "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION", |
| 230 | "InstallationFolder", path, &sdkVersion); |
| 231 | if (!sdkVersion.empty()) |
Reid Kleckner | 6b7156b | 2015-01-23 19:16:25 +0000 | [diff] [blame] | 232 | std::sscanf(sdkVersion.c_str(), "v%d.%d", &major, &minor); |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 233 | return hasSDKDir && !path.empty(); |
| 234 | } |
| 235 | |
Zachary Turner | 10d75b2 | 2014-10-22 20:40:43 +0000 | [diff] [blame] | 236 | // Gets the library path required to link against the Windows SDK. |
| 237 | bool MSVCToolChain::getWindowsSDKLibraryPath(std::string &path) const { |
| 238 | std::string sdkPath; |
| 239 | int sdkMajor = 0; |
| 240 | int sdkMinor = 0; |
| 241 | |
| 242 | path.clear(); |
| 243 | if (!getWindowsSDKDir(sdkPath, sdkMajor, sdkMinor)) |
| 244 | return false; |
| 245 | |
| 246 | llvm::SmallString<128> libPath(sdkPath); |
| 247 | llvm::sys::path::append(libPath, "Lib"); |
| 248 | if (sdkMajor <= 7) { |
| 249 | switch (getArch()) { |
| 250 | // In Windows SDK 7.x, x86 libraries are directly in the Lib folder. |
| 251 | case llvm::Triple::x86: |
| 252 | break; |
| 253 | case llvm::Triple::x86_64: |
| 254 | llvm::sys::path::append(libPath, "x64"); |
| 255 | break; |
| 256 | case llvm::Triple::arm: |
| 257 | // It is not necessary to link against Windows SDK 7.x when targeting ARM. |
| 258 | return false; |
| 259 | default: |
| 260 | return false; |
| 261 | } |
| 262 | } else { |
| 263 | // Windows SDK 8.x installs libraries in a folder whose names depend on the |
| 264 | // version of the OS you're targeting. By default choose the newest, which |
| 265 | // usually corresponds to the version of the OS you've installed the SDK on. |
Zachary Turner | 34eb943 | 2014-10-22 21:48:56 +0000 | [diff] [blame] | 266 | const char *tests[] = {"winv6.3", "win8", "win7"}; |
Zachary Turner | 10d75b2 | 2014-10-22 20:40:43 +0000 | [diff] [blame] | 267 | bool found = false; |
Zachary Turner | 34eb943 | 2014-10-22 21:48:56 +0000 | [diff] [blame] | 268 | for (const char *test : tests) { |
Zachary Turner | 10d75b2 | 2014-10-22 20:40:43 +0000 | [diff] [blame] | 269 | llvm::SmallString<128> testPath(libPath); |
| 270 | llvm::sys::path::append(testPath, test); |
| 271 | if (llvm::sys::fs::exists(testPath.c_str())) { |
| 272 | libPath = testPath; |
| 273 | found = true; |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (!found) |
| 279 | return false; |
| 280 | |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame^] | 281 | const StringRef archName = getWindowsSDKArch(getArch()); |
| 282 | if (archName.empty()) |
Zachary Turner | 10d75b2 | 2014-10-22 20:40:43 +0000 | [diff] [blame] | 283 | return false; |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame^] | 284 | llvm::sys::path::append(libPath, "um", archName); |
Zachary Turner | 10d75b2 | 2014-10-22 20:40:43 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | path = libPath.str(); |
| 288 | return true; |
| 289 | } |
| 290 | |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame^] | 291 | // Check if the Include path of a specified version of Visual Studio contains |
| 292 | // specific header files. If not, they are probably shipped with Universal CRT. |
| 293 | bool clang::driver::toolchains::MSVCToolChain::useUniversalCRT( |
| 294 | std::string &VisualStudioDir) const { |
| 295 | llvm::SmallString<128> TestPath(VisualStudioDir); |
| 296 | llvm::sys::path::append(TestPath, "VC\\include\\stdlib.h"); |
| 297 | |
| 298 | return !llvm::sys::fs::exists(TestPath); |
| 299 | } |
| 300 | |
| 301 | bool MSVCToolChain::getUniversalCRTSdkDir(std::string &Path, |
| 302 | std::string &UCRTVersion) const { |
| 303 | // vcvarsqueryregistry.bat for Visual Studio 2015 queries the registry |
| 304 | // for the specific key "KitsRoot10". So do we. |
| 305 | if (!getSystemRegistryString( |
| 306 | "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots", "KitsRoot10", |
| 307 | Path, nullptr)) |
| 308 | return false; |
| 309 | |
| 310 | UCRTVersion.clear(); |
| 311 | |
| 312 | // Find the most recent version of Universal CRT. |
| 313 | // vcvarsqueryregistry.bat sorts entries in the include directory by names and |
| 314 | // uses the last one of the list. |
| 315 | // So we compare entry names lexicographically to find the greatest one. |
| 316 | std::error_code EC; |
| 317 | llvm::SmallString<128> IncludePath(Path); |
| 318 | llvm::sys::path::append(IncludePath, "Include"); |
| 319 | for (llvm::sys::fs::directory_iterator DirIt(IncludePath, EC), DirEnd; |
| 320 | DirIt != DirEnd && !EC; DirIt.increment(EC)) { |
| 321 | if (!llvm::sys::fs::is_directory(DirIt->path())) |
| 322 | continue; |
| 323 | StringRef CandidateName = llvm::sys::path::filename(DirIt->path()); |
| 324 | if (CandidateName > UCRTVersion) |
| 325 | UCRTVersion = CandidateName; |
| 326 | } |
| 327 | |
| 328 | return !UCRTVersion.empty(); |
| 329 | } |
| 330 | |
| 331 | bool MSVCToolChain::getUniversalCRTLibraryPath(std::string &Path) const { |
| 332 | std::string UniversalCRTSdkPath; |
| 333 | std::string UCRTVersion; |
| 334 | |
| 335 | Path.clear(); |
| 336 | if (!getUniversalCRTSdkDir(UniversalCRTSdkPath, UCRTVersion)) |
| 337 | return false; |
| 338 | |
| 339 | StringRef ArchName = getWindowsSDKArch(getArch()); |
| 340 | if (ArchName.empty()) |
| 341 | return false; |
| 342 | |
| 343 | llvm::SmallString<128> LibPath(UniversalCRTSdkPath); |
| 344 | llvm::sys::path::append(LibPath, "Lib", UCRTVersion, "ucrt", ArchName); |
| 345 | |
| 346 | Path = LibPath.str(); |
| 347 | return true; |
| 348 | } |
| 349 | |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 350 | // Get the location to use for Visual Studio binaries. The location priority |
| 351 | // is: %VCINSTALLDIR% > %PATH% > newest copy of Visual Studio installed on |
| 352 | // system (as reported by the registry). |
| 353 | bool MSVCToolChain::getVisualStudioBinariesFolder(const char *clangProgramPath, |
| 354 | std::string &path) const { |
| 355 | path.clear(); |
| 356 | |
| 357 | SmallString<128> BinDir; |
| 358 | |
| 359 | // First check the environment variables that vsvars32.bat sets. |
| 360 | llvm::Optional<std::string> VcInstallDir = |
| 361 | llvm::sys::Process::GetEnv("VCINSTALLDIR"); |
| 362 | if (VcInstallDir.hasValue()) { |
| 363 | BinDir = VcInstallDir.getValue(); |
| 364 | llvm::sys::path::append(BinDir, "bin"); |
| 365 | } else { |
| 366 | // Next walk the PATH, trying to find a cl.exe in the path. If we find one, |
| 367 | // use that. However, make sure it's not clang's cl.exe. |
| 368 | llvm::Optional<std::string> OptPath = llvm::sys::Process::GetEnv("PATH"); |
| 369 | if (OptPath.hasValue()) { |
| 370 | const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'}; |
| 371 | SmallVector<StringRef, 8> PathSegments; |
| 372 | llvm::SplitString(OptPath.getValue(), PathSegments, EnvPathSeparatorStr); |
| 373 | |
| 374 | for (StringRef PathSegment : PathSegments) { |
| 375 | if (PathSegment.empty()) |
| 376 | continue; |
| 377 | |
| 378 | SmallString<128> FilePath(PathSegment); |
| 379 | llvm::sys::path::append(FilePath, "cl.exe"); |
| 380 | if (llvm::sys::fs::can_execute(FilePath.c_str()) && |
| 381 | !llvm::sys::fs::equivalent(FilePath.c_str(), clangProgramPath)) { |
| 382 | // If we found it on the PATH, use it exactly as is with no |
| 383 | // modifications. |
| 384 | path = PathSegment; |
| 385 | return true; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | std::string installDir; |
| 391 | // With no VCINSTALLDIR and nothing on the PATH, if we can't find it in the |
| 392 | // registry then we have no choice but to fail. |
| 393 | if (!getVisualStudioInstallDir(installDir)) |
| 394 | return false; |
| 395 | |
| 396 | // Regardless of what binary we're ultimately trying to find, we make sure |
| 397 | // that this is a Visual Studio directory by checking for cl.exe. We use |
| 398 | // cl.exe instead of other binaries like link.exe because programs such as |
| 399 | // GnuWin32 also have a utility called link.exe, so cl.exe is the least |
| 400 | // ambiguous. |
| 401 | BinDir = installDir; |
| 402 | llvm::sys::path::append(BinDir, "VC", "bin"); |
| 403 | SmallString<128> ClPath(BinDir); |
| 404 | llvm::sys::path::append(ClPath, "cl.exe"); |
| 405 | |
| 406 | if (!llvm::sys::fs::can_execute(ClPath.c_str())) |
| 407 | return false; |
Hans Wennborg | e6b994e | 2014-10-20 23:26:03 +0000 | [diff] [blame] | 408 | } |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 409 | |
| 410 | if (BinDir.empty()) |
| 411 | return false; |
| 412 | |
| 413 | switch (getArch()) { |
| 414 | case llvm::Triple::x86: |
| 415 | break; |
| 416 | case llvm::Triple::x86_64: |
| 417 | llvm::sys::path::append(BinDir, "amd64"); |
| 418 | break; |
| 419 | case llvm::Triple::arm: |
| 420 | llvm::sys::path::append(BinDir, "arm"); |
| 421 | break; |
| 422 | default: |
| 423 | // Whatever this is, Visual Studio doesn't have a toolchain for it. |
| 424 | return false; |
| 425 | } |
| 426 | path = BinDir.str(); |
| 427 | return true; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Alp Toker | fcce183 | 2014-06-22 03:27:45 +0000 | [diff] [blame] | 430 | // Get Visual Studio installation directory. |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 431 | bool MSVCToolChain::getVisualStudioInstallDir(std::string &path) const { |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 432 | // First check the environment variables that vsvars32.bat sets. |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 433 | const char *vcinstalldir = getenv("VCINSTALLDIR"); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 434 | if (vcinstalldir) { |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 435 | path = vcinstalldir; |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 436 | path = path.substr(0, path.find("\\VC")); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 437 | return true; |
| 438 | } |
| 439 | |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 440 | std::string vsIDEInstallDir; |
| 441 | std::string vsExpressIDEInstallDir; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 442 | // Then try the windows registry. |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 443 | bool hasVCDir = |
| 444 | getSystemRegistryString("SOFTWARE\\Microsoft\\VisualStudio\\$VERSION", |
| 445 | "InstallDir", vsIDEInstallDir, nullptr); |
| 446 | if (hasVCDir && !vsIDEInstallDir.empty()) { |
| 447 | path = vsIDEInstallDir.substr(0, vsIDEInstallDir.find("\\Common7\\IDE")); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 448 | return true; |
| 449 | } |
| 450 | |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 451 | bool hasVCExpressDir = |
| 452 | getSystemRegistryString("SOFTWARE\\Microsoft\\VCExpress\\$VERSION", |
| 453 | "InstallDir", vsExpressIDEInstallDir, nullptr); |
| 454 | if (hasVCExpressDir && !vsExpressIDEInstallDir.empty()) { |
| 455 | path = vsExpressIDEInstallDir.substr( |
| 456 | 0, vsIDEInstallDir.find("\\Common7\\IDE")); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 457 | return true; |
| 458 | } |
| 459 | |
| 460 | // Try the environment. |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 461 | const char *vs120comntools = getenv("VS120COMNTOOLS"); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 462 | const char *vs100comntools = getenv("VS100COMNTOOLS"); |
| 463 | const char *vs90comntools = getenv("VS90COMNTOOLS"); |
| 464 | const char *vs80comntools = getenv("VS80COMNTOOLS"); |
Alp Toker | fcce183 | 2014-06-22 03:27:45 +0000 | [diff] [blame] | 465 | |
| 466 | const char *vscomntools = nullptr; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 467 | |
Alp Toker | a207440 | 2014-06-22 03:27:52 +0000 | [diff] [blame] | 468 | // Find any version we can |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 469 | if (vs120comntools) |
| 470 | vscomntools = vs120comntools; |
| 471 | else if (vs100comntools) |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 472 | vscomntools = vs100comntools; |
| 473 | else if (vs90comntools) |
| 474 | vscomntools = vs90comntools; |
| 475 | else if (vs80comntools) |
| 476 | vscomntools = vs80comntools; |
| 477 | |
| 478 | if (vscomntools && *vscomntools) { |
| 479 | const char *p = strstr(vscomntools, "\\Common7\\Tools"); |
| 480 | path = p ? std::string(vscomntools, p) : vscomntools; |
| 481 | return true; |
| 482 | } |
| 483 | return false; |
| 484 | } |
| 485 | |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 486 | void MSVCToolChain::AddSystemIncludeWithSubfolder(const ArgList &DriverArgs, |
| 487 | ArgStringList &CC1Args, |
| 488 | const std::string &folder, |
| 489 | const char *subfolder) const { |
| 490 | llvm::SmallString<128> path(folder); |
| 491 | llvm::sys::path::append(path, subfolder); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 492 | addSystemInclude(DriverArgs, CC1Args, path); |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 495 | void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 496 | ArgStringList &CC1Args) const { |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 497 | if (DriverArgs.hasArg(options::OPT_nostdinc)) |
| 498 | return; |
| 499 | |
| 500 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
Rafael Espindola | 76565f0 | 2013-06-26 03:39:10 +0000 | [diff] [blame] | 501 | SmallString<128> P(getDriver().ResourceDir); |
| 502 | llvm::sys::path::append(P, "include"); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 503 | addSystemInclude(DriverArgs, CC1Args, P); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
| 507 | return; |
| 508 | |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 509 | // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat. |
| 510 | if (const char *cl_include_dir = getenv("INCLUDE")) { |
| 511 | SmallVector<StringRef, 8> Dirs; |
Reid Kleckner | 77b45ba | 2014-04-23 00:15:01 +0000 | [diff] [blame] | 512 | StringRef(cl_include_dir) |
| 513 | .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
| 514 | for (StringRef Dir : Dirs) |
| 515 | addSystemInclude(DriverArgs, CC1Args, Dir); |
| 516 | if (!Dirs.empty()) |
| 517 | return; |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | std::string VSDir; |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 521 | |
| 522 | // When built with access to the proper Windows APIs, try to actually find |
| 523 | // the correct include paths first. |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 524 | if (getVisualStudioInstallDir(VSDir)) { |
| 525 | AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, VSDir, "VC\\include"); |
| 526 | |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame^] | 527 | if (useUniversalCRT(VSDir)) { |
| 528 | std::string UniversalCRTSdkPath; |
| 529 | std::string UCRTVersion; |
| 530 | if (getUniversalCRTSdkDir(UniversalCRTSdkPath, UCRTVersion)) { |
| 531 | llvm::SmallString<128> UCRTIncludePath(UniversalCRTSdkPath); |
| 532 | llvm::sys::path::append(UCRTIncludePath, "Include", UCRTVersion, |
| 533 | "ucrt"); |
| 534 | addSystemInclude(DriverArgs, CC1Args, UCRTIncludePath); |
| 535 | } |
| 536 | } |
| 537 | |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 538 | std::string WindowsSDKDir; |
| 539 | int major, minor; |
| 540 | if (getWindowsSDKDir(WindowsSDKDir, major, minor)) { |
| 541 | if (major >= 8) { |
| 542 | AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir, |
| 543 | "include\\shared"); |
| 544 | AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir, |
| 545 | "include\\um"); |
| 546 | AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir, |
| 547 | "include\\winrt"); |
| 548 | } else { |
| 549 | AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir, |
| 550 | "include"); |
| 551 | } |
Reid Kleckner | 77b45ba | 2014-04-23 00:15:01 +0000 | [diff] [blame] | 552 | } else { |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 553 | addSystemInclude(DriverArgs, CC1Args, VSDir); |
Reid Kleckner | 77b45ba | 2014-04-23 00:15:01 +0000 | [diff] [blame] | 554 | } |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 555 | return; |
| 556 | } |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 557 | |
| 558 | // As a fallback, select default install paths. |
Alp Toker | fcce183 | 2014-06-22 03:27:45 +0000 | [diff] [blame] | 559 | // FIXME: Don't guess drives and paths like this on Windows. |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 560 | const StringRef Paths[] = { |
| 561 | "C:/Program Files/Microsoft Visual Studio 10.0/VC/include", |
| 562 | "C:/Program Files/Microsoft Visual Studio 9.0/VC/include", |
| 563 | "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include", |
| 564 | "C:/Program Files/Microsoft Visual Studio 8/VC/include", |
| 565 | "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include" |
| 566 | }; |
| 567 | addSystemIncludes(DriverArgs, CC1Args, Paths); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 570 | void MSVCToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, |
| 571 | ArgStringList &CC1Args) const { |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 572 | // FIXME: There should probably be logic here to find libc++ on Windows. |
| 573 | } |
David Majnemer | e11d373 | 2015-06-08 00:22:46 +0000 | [diff] [blame] | 574 | |
| 575 | std::string |
| 576 | MSVCToolChain::ComputeEffectiveClangTriple(const ArgList &Args, |
| 577 | types::ID InputType) const { |
| 578 | std::string TripleStr = |
| 579 | ToolChain::ComputeEffectiveClangTriple(Args, InputType); |
| 580 | llvm::Triple Triple(TripleStr); |
| 581 | VersionTuple MSVT = |
| 582 | tools::visualstudio::getMSVCVersion(/*D=*/nullptr, Triple, Args, |
| 583 | /*IsWindowsMSVC=*/true); |
| 584 | if (MSVT.empty()) |
| 585 | return TripleStr; |
| 586 | |
| 587 | MSVT = VersionTuple(MSVT.getMajor(), MSVT.getMinor().getValueOr(0), |
| 588 | MSVT.getSubminor().getValueOr(0)); |
| 589 | |
David Majnemer | 75fdd6b | 2015-06-09 06:30:01 +0000 | [diff] [blame] | 590 | if (Triple.getEnvironment() == llvm::Triple::MSVC) { |
| 591 | StringRef ObjFmt = Triple.getEnvironmentName().split('-').second; |
| 592 | if (ObjFmt.empty()) |
| 593 | Triple.setEnvironmentName((Twine("msvc") + MSVT.getAsString()).str()); |
| 594 | else |
| 595 | Triple.setEnvironmentName( |
| 596 | (Twine("msvc") + MSVT.getAsString() + Twine('-') + ObjFmt).str()); |
| 597 | } |
David Majnemer | e11d373 | 2015-06-08 00:22:46 +0000 | [diff] [blame] | 598 | return Triple.getTriple(); |
| 599 | } |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 600 | |
| 601 | SanitizerMask MSVCToolChain::getSupportedSanitizers() const { |
| 602 | SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
| 603 | Res |= SanitizerKind::Address; |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 604 | return Res; |
| 605 | } |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 606 | |
| 607 | llvm::opt::DerivedArgList * |
| 608 | MSVCToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args, |
| 609 | const char *BoundArch) const { |
| 610 | DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs()); |
| 611 | const OptTable &Opts = getDriver().getOpts(); |
| 612 | |
David Majnemer | 7ab76f2 | 2015-08-25 00:46:45 +0000 | [diff] [blame] | 613 | // /Oy and /Oy- only has an effect under X86-32. |
| 614 | bool SupportsForcingFramePointer = getArch() == llvm::Triple::x86; |
| 615 | |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 616 | // The -O[12xd] flag actually expands to several flags. We must desugar the |
| 617 | // flags so that options embedded can be negated. For example, the '-O2' flag |
| 618 | // enables '-Oy'. Expanding '-O2' into its constituent flags allows us to |
| 619 | // correctly handle '-O2 -Oy-' where the trailing '-Oy-' disables a single |
| 620 | // aspect of '-O2'. |
| 621 | // |
| 622 | // Note that this expansion logic only applies to the *last* of '[12xd]'. |
| 623 | |
| 624 | // First step is to search for the character we'd like to expand. |
| 625 | const char *ExpandChar = nullptr; |
| 626 | for (Arg *A : Args) { |
| 627 | if (!A->getOption().matches(options::OPT__SLASH_O)) |
| 628 | continue; |
| 629 | StringRef OptStr = A->getValue(); |
| 630 | for (size_t I = 0, E = OptStr.size(); I != E; ++I) { |
| 631 | const char &OptChar = *(OptStr.data() + I); |
| 632 | if (OptChar == '1' || OptChar == '2' || OptChar == 'x' || OptChar == 'd') |
| 633 | ExpandChar = OptStr.data() + I; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | // The -O flag actually takes an amalgam of other options. For example, |
| 638 | // '/Ogyb2' is equivalent to '/Og' '/Oy' '/Ob2'. |
| 639 | for (Arg *A : Args) { |
| 640 | if (!A->getOption().matches(options::OPT__SLASH_O)) { |
| 641 | DAL->append(A); |
| 642 | continue; |
| 643 | } |
| 644 | |
| 645 | StringRef OptStr = A->getValue(); |
| 646 | for (size_t I = 0, E = OptStr.size(); I != E; ++I) { |
| 647 | const char &OptChar = *(OptStr.data() + I); |
| 648 | switch (OptChar) { |
| 649 | default: |
| 650 | break; |
| 651 | case '1': |
| 652 | case '2': |
| 653 | case 'x': |
| 654 | case 'd': |
| 655 | if (&OptChar == ExpandChar) { |
| 656 | if (OptChar == 'd') { |
| 657 | DAL->AddFlagArg(A, Opts.getOption(options::OPT_O0)); |
| 658 | } else { |
| 659 | if (OptChar == '1') { |
| 660 | DAL->AddJoinedArg(A, Opts.getOption(options::OPT_O), "s"); |
| 661 | } else if (OptChar == '2' || OptChar == 'x') { |
| 662 | DAL->AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin)); |
| 663 | DAL->AddJoinedArg(A, Opts.getOption(options::OPT_O), "2"); |
| 664 | } |
David Majnemer | 7ab76f2 | 2015-08-25 00:46:45 +0000 | [diff] [blame] | 665 | if (SupportsForcingFramePointer) |
| 666 | DAL->AddFlagArg(A, |
| 667 | Opts.getOption(options::OPT_fomit_frame_pointer)); |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 668 | if (OptChar == '1' || OptChar == '2') |
| 669 | DAL->AddFlagArg(A, |
| 670 | Opts.getOption(options::OPT_ffunction_sections)); |
| 671 | } |
| 672 | } |
| 673 | break; |
| 674 | case 'b': |
David Majnemer | 7ab76f2 | 2015-08-25 00:46:45 +0000 | [diff] [blame] | 675 | if (I + 1 != E && isdigit(OptStr[I + 1])) |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 676 | ++I; |
| 677 | break; |
| 678 | case 'g': |
| 679 | break; |
| 680 | case 'i': |
David Majnemer | 7ab76f2 | 2015-08-25 00:46:45 +0000 | [diff] [blame] | 681 | if (I + 1 != E && OptStr[I + 1] == '-') { |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 682 | ++I; |
| 683 | DAL->AddFlagArg(A, Opts.getOption(options::OPT_fno_builtin)); |
| 684 | } else { |
| 685 | DAL->AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin)); |
| 686 | } |
| 687 | break; |
| 688 | case 's': |
| 689 | DAL->AddJoinedArg(A, Opts.getOption(options::OPT_O), "s"); |
| 690 | break; |
| 691 | case 't': |
| 692 | DAL->AddJoinedArg(A, Opts.getOption(options::OPT_O), "2"); |
| 693 | break; |
David Majnemer | 7ab76f2 | 2015-08-25 00:46:45 +0000 | [diff] [blame] | 694 | case 'y': { |
| 695 | bool OmitFramePointer = true; |
| 696 | if (I + 1 != E && OptStr[I + 1] == '-') { |
| 697 | OmitFramePointer = false; |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 698 | ++I; |
David Majnemer | 7ab76f2 | 2015-08-25 00:46:45 +0000 | [diff] [blame] | 699 | } |
| 700 | if (SupportsForcingFramePointer) { |
| 701 | if (OmitFramePointer) |
| 702 | DAL->AddFlagArg(A, |
| 703 | Opts.getOption(options::OPT_fomit_frame_pointer)); |
| 704 | else |
| 705 | DAL->AddFlagArg( |
| 706 | A, Opts.getOption(options::OPT_fno_omit_frame_pointer)); |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 707 | } |
| 708 | break; |
| 709 | } |
David Majnemer | 7ab76f2 | 2015-08-25 00:46:45 +0000 | [diff] [blame] | 710 | } |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | return DAL; |
| 714 | } |