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