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