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