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