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