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