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" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 17 | #include "llvm/Option/Arg.h" |
| 18 | #include "llvm/Option/ArgList.h" |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
| 20 | #include "llvm/Support/Path.h" |
| 21 | |
| 22 | // Include the necessary headers to interface with the Windows registry and |
| 23 | // environment. |
| 24 | #ifdef _MSC_VER |
| 25 | #define WIN32_LEAN_AND_MEAN |
| 26 | #define NOGDI |
| 27 | #define NOMINMAX |
| 28 | #include <Windows.h> |
| 29 | #endif |
| 30 | |
| 31 | using namespace clang::driver; |
| 32 | using namespace clang::driver::toolchains; |
| 33 | using namespace clang; |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 34 | using namespace llvm::opt; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 35 | |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 36 | Windows::Windows(const Driver &D, const llvm::Triple& Triple, |
| 37 | const ArgList &Args) |
| 38 | : ToolChain(D, Triple, Args) { |
| 39 | } |
| 40 | |
| 41 | Tool *Windows::buildLinker() const { |
| 42 | return new tools::visualstudio::Link(*this); |
| 43 | } |
| 44 | |
| 45 | Tool *Windows::buildAssembler() const { |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 46 | if (getTriple().isOSBinFormatMachO()) |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 47 | return new tools::darwin::Assemble(*this); |
Alp Toker | c8d4f0f | 2013-11-22 08:27:46 +0000 | [diff] [blame] | 48 | getDriver().Diag(clang::diag::err_no_external_assembler); |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | bool Windows::IsIntegratedAssemblerDefault() const { |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | bool Windows::IsUnwindTablesDefault() const { |
| 57 | return getArch() == llvm::Triple::x86_64; |
| 58 | } |
| 59 | |
| 60 | bool Windows::isPICDefault() const { |
| 61 | return getArch() == llvm::Triple::x86_64; |
| 62 | } |
| 63 | |
| 64 | bool Windows::isPIEDefault() const { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | bool Windows::isPICDefaultForced() const { |
| 69 | return getArch() == llvm::Triple::x86_64; |
| 70 | } |
| 71 | |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 72 | // FIXME: This probably should goto to some platform utils place. |
| 73 | #ifdef _MSC_VER |
| 74 | |
| 75 | /// \brief Read registry string. |
| 76 | /// This also supports a means to look for high-versioned keys by use |
| 77 | /// of a $VERSION placeholder in the key path. |
| 78 | /// $VERSION in the key path is a placeholder for the version number, |
| 79 | /// causing the highest value path to be searched for and used. |
| 80 | /// I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION". |
| 81 | /// There can be additional characters in the component. Only the numberic |
| 82 | /// characters are compared. |
| 83 | static bool getSystemRegistryString(const char *keyPath, const char *valueName, |
| 84 | char *value, size_t maxLength) { |
| 85 | HKEY hRootKey = NULL; |
| 86 | HKEY hKey = NULL; |
| 87 | const char* subKey = NULL; |
| 88 | DWORD valueType; |
| 89 | DWORD valueSize = maxLength - 1; |
| 90 | long lResult; |
| 91 | bool returnValue = false; |
| 92 | |
| 93 | if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) { |
| 94 | hRootKey = HKEY_CLASSES_ROOT; |
| 95 | subKey = keyPath + 18; |
| 96 | } else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) { |
| 97 | hRootKey = HKEY_USERS; |
| 98 | subKey = keyPath + 11; |
| 99 | } else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) { |
| 100 | hRootKey = HKEY_LOCAL_MACHINE; |
| 101 | subKey = keyPath + 19; |
| 102 | } else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) { |
| 103 | hRootKey = HKEY_CURRENT_USER; |
| 104 | subKey = keyPath + 18; |
| 105 | } else { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | const char *placeHolder = strstr(subKey, "$VERSION"); |
| 110 | char bestName[256]; |
| 111 | bestName[0] = '\0'; |
| 112 | // If we have a $VERSION placeholder, do the highest-version search. |
| 113 | if (placeHolder) { |
| 114 | const char *keyEnd = placeHolder - 1; |
| 115 | const char *nextKey = placeHolder; |
| 116 | // Find end of previous key. |
| 117 | while ((keyEnd > subKey) && (*keyEnd != '\\')) |
| 118 | keyEnd--; |
| 119 | // Find end of key containing $VERSION. |
| 120 | while (*nextKey && (*nextKey != '\\')) |
| 121 | nextKey++; |
| 122 | size_t partialKeyLength = keyEnd - subKey; |
| 123 | char partialKey[256]; |
| 124 | if (partialKeyLength > sizeof(partialKey)) |
| 125 | partialKeyLength = sizeof(partialKey); |
| 126 | strncpy(partialKey, subKey, partialKeyLength); |
| 127 | partialKey[partialKeyLength] = '\0'; |
| 128 | HKEY hTopKey = NULL; |
Hans Wennborg | 935d01d | 2013-10-09 23:41:48 +0000 | [diff] [blame] | 129 | lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY, |
| 130 | &hTopKey); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 131 | if (lResult == ERROR_SUCCESS) { |
| 132 | char keyName[256]; |
| 133 | int bestIndex = -1; |
| 134 | double bestValue = 0.0; |
| 135 | DWORD index, size = sizeof(keyName) - 1; |
| 136 | for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL, |
| 137 | NULL, NULL, NULL) == ERROR_SUCCESS; index++) { |
| 138 | const char *sp = keyName; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 139 | while (*sp && !isDigit(*sp)) |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 140 | sp++; |
| 141 | if (!*sp) |
| 142 | continue; |
| 143 | const char *ep = sp + 1; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 144 | while (*ep && (isDigit(*ep) || (*ep == '.'))) |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 145 | ep++; |
| 146 | char numBuf[32]; |
| 147 | strncpy(numBuf, sp, sizeof(numBuf) - 1); |
| 148 | numBuf[sizeof(numBuf) - 1] = '\0'; |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 149 | double dvalue = strtod(numBuf, NULL); |
| 150 | if (dvalue > bestValue) { |
| 151 | // Test that InstallDir is indeed there before keeping this index. |
| 152 | // Open the chosen key path remainder. |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 153 | strcpy(bestName, keyName); |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 154 | // Append rest of key. |
| 155 | strncat(bestName, nextKey, sizeof(bestName) - 1); |
| 156 | bestName[sizeof(bestName) - 1] = '\0'; |
| 157 | lResult = RegOpenKeyEx(hTopKey, bestName, 0, |
| 158 | KEY_READ | KEY_WOW64_32KEY, &hKey); |
| 159 | if (lResult == ERROR_SUCCESS) { |
| 160 | lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType, |
| 161 | (LPBYTE)value, &valueSize); |
| 162 | if (lResult == ERROR_SUCCESS) { |
| 163 | bestIndex = (int)index; |
| 164 | bestValue = dvalue; |
| 165 | returnValue = true; |
| 166 | } |
| 167 | RegCloseKey(hKey); |
| 168 | } |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 169 | } |
| 170 | size = sizeof(keyName) - 1; |
| 171 | } |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 172 | RegCloseKey(hTopKey); |
| 173 | } |
| 174 | } else { |
Hans Wennborg | 935d01d | 2013-10-09 23:41:48 +0000 | [diff] [blame] | 175 | lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ | KEY_WOW64_32KEY, |
| 176 | &hKey); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 177 | if (lResult == ERROR_SUCCESS) { |
| 178 | lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType, |
| 179 | (LPBYTE)value, &valueSize); |
| 180 | if (lResult == ERROR_SUCCESS) |
| 181 | returnValue = true; |
| 182 | RegCloseKey(hKey); |
| 183 | } |
| 184 | } |
| 185 | return returnValue; |
| 186 | } |
| 187 | |
| 188 | /// \brief Get Windows SDK installation directory. |
| 189 | static bool getWindowsSDKDir(std::string &path) { |
| 190 | char windowsSDKInstallDir[256]; |
| 191 | // Try the Windows registry. |
| 192 | bool hasSDKDir = getSystemRegistryString( |
| 193 | "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION", |
| 194 | "InstallationFolder", |
| 195 | windowsSDKInstallDir, |
| 196 | sizeof(windowsSDKInstallDir) - 1); |
| 197 | // If we have both vc80 and vc90, pick version we were compiled with. |
| 198 | if (hasSDKDir && windowsSDKInstallDir[0]) { |
| 199 | path = windowsSDKInstallDir; |
| 200 | return true; |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 205 | // Get Visual Studio installation directory. |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 206 | static bool getVisualStudioDir(std::string &path) { |
| 207 | // First check the environment variables that vsvars32.bat sets. |
| 208 | const char* vcinstalldir = getenv("VCINSTALLDIR"); |
| 209 | if (vcinstalldir) { |
| 210 | char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC")); |
| 211 | if (p) |
| 212 | *p = '\0'; |
| 213 | path = vcinstalldir; |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | char vsIDEInstallDir[256]; |
| 218 | char vsExpressIDEInstallDir[256]; |
| 219 | // Then try the windows registry. |
| 220 | bool hasVCDir = getSystemRegistryString( |
| 221 | "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION", |
| 222 | "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1); |
| 223 | bool hasVCExpressDir = getSystemRegistryString( |
| 224 | "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION", |
| 225 | "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1); |
| 226 | // If we have both vc80 and vc90, pick version we were compiled with. |
| 227 | if (hasVCDir && vsIDEInstallDir[0]) { |
| 228 | char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE"); |
| 229 | if (p) |
| 230 | *p = '\0'; |
| 231 | path = vsIDEInstallDir; |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | if (hasVCExpressDir && vsExpressIDEInstallDir[0]) { |
| 236 | char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE"); |
| 237 | if (p) |
| 238 | *p = '\0'; |
| 239 | path = vsExpressIDEInstallDir; |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | // Try the environment. |
| 244 | const char *vs100comntools = getenv("VS100COMNTOOLS"); |
| 245 | const char *vs90comntools = getenv("VS90COMNTOOLS"); |
| 246 | const char *vs80comntools = getenv("VS80COMNTOOLS"); |
| 247 | const char *vscomntools = NULL; |
| 248 | |
| 249 | // Try to find the version that we were compiled with |
| 250 | if(false) {} |
| 251 | #if (_MSC_VER >= 1600) // VC100 |
| 252 | else if(vs100comntools) { |
| 253 | vscomntools = vs100comntools; |
| 254 | } |
| 255 | #elif (_MSC_VER == 1500) // VC80 |
| 256 | else if(vs90comntools) { |
| 257 | vscomntools = vs90comntools; |
| 258 | } |
| 259 | #elif (_MSC_VER == 1400) // VC80 |
| 260 | else if(vs80comntools) { |
| 261 | vscomntools = vs80comntools; |
| 262 | } |
| 263 | #endif |
| 264 | // Otherwise find any version we can |
| 265 | else if (vs100comntools) |
| 266 | vscomntools = vs100comntools; |
| 267 | else if (vs90comntools) |
| 268 | vscomntools = vs90comntools; |
| 269 | else if (vs80comntools) |
| 270 | vscomntools = vs80comntools; |
| 271 | |
| 272 | if (vscomntools && *vscomntools) { |
| 273 | const char *p = strstr(vscomntools, "\\Common7\\Tools"); |
| 274 | path = p ? std::string(vscomntools, p) : vscomntools; |
| 275 | return true; |
| 276 | } |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | #endif // _MSC_VER |
| 281 | |
| 282 | void Windows::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 283 | ArgStringList &CC1Args) const { |
| 284 | if (DriverArgs.hasArg(options::OPT_nostdinc)) |
| 285 | return; |
| 286 | |
| 287 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
Rafael Espindola | 76565f0 | 2013-06-26 03:39:10 +0000 | [diff] [blame] | 288 | SmallString<128> P(getDriver().ResourceDir); |
| 289 | llvm::sys::path::append(P, "include"); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 290 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
| 291 | } |
| 292 | |
| 293 | if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
| 294 | return; |
| 295 | |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 296 | #ifdef _MSC_VER |
| 297 | // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat. |
| 298 | if (const char *cl_include_dir = getenv("INCLUDE")) { |
| 299 | SmallVector<StringRef, 8> Dirs; |
Reid Kleckner | 77b45ba | 2014-04-23 00:15:01 +0000 | [diff] [blame^] | 300 | StringRef(cl_include_dir) |
| 301 | .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
| 302 | for (StringRef Dir : Dirs) |
| 303 | addSystemInclude(DriverArgs, CC1Args, Dir); |
| 304 | if (!Dirs.empty()) |
| 305 | return; |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | std::string VSDir; |
| 309 | std::string WindowsSDKDir; |
| 310 | |
| 311 | // When built with access to the proper Windows APIs, try to actually find |
| 312 | // the correct include paths first. |
| 313 | if (getVisualStudioDir(VSDir)) { |
Reid Kleckner | 77b45ba | 2014-04-23 00:15:01 +0000 | [diff] [blame^] | 314 | SmallString<128> P; |
| 315 | P = VSDir; |
| 316 | llvm::sys::path::append(P, "VC\\include"); |
| 317 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
| 318 | if (getWindowsSDKDir(WindowsSDKDir)) { |
| 319 | P = WindowsSDKDir; |
| 320 | llvm::sys::path::append(P, "include"); |
| 321 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
| 322 | } else { |
| 323 | P = VSDir; |
| 324 | llvm::sys::path::append(P, "VC\\PlatformSDK\\Include"); |
| 325 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
| 326 | } |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 327 | return; |
| 328 | } |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 329 | |
| 330 | // As a fallback, select default install paths. |
| 331 | const StringRef Paths[] = { |
| 332 | "C:/Program Files/Microsoft Visual Studio 10.0/VC/include", |
| 333 | "C:/Program Files/Microsoft Visual Studio 9.0/VC/include", |
| 334 | "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include", |
| 335 | "C:/Program Files/Microsoft Visual Studio 8/VC/include", |
| 336 | "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include" |
| 337 | }; |
| 338 | addSystemIncludes(DriverArgs, CC1Args, Paths); |
David Majnemer | 04a8357 | 2014-01-31 01:35:55 +0000 | [diff] [blame] | 339 | #endif // _MSC_VER |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void Windows::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, |
| 343 | ArgStringList &CC1Args) const { |
| 344 | // FIXME: There should probably be logic here to find libc++ on Windows. |
| 345 | } |