WindowsToolChain.cpp revision 263508
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#include "clang/Basic/CharInfo.h" 12#include "clang/Basic/Version.h" 13#include "clang/Driver/Compilation.h" 14#include "clang/Driver/Driver.h" 15#include "clang/Driver/DriverDiagnostic.h" 16#include "clang/Driver/Options.h" 17#include "llvm/Option/Arg.h" 18#include "llvm/Option/ArgList.h" 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 31using namespace clang::driver; 32using namespace clang::driver::toolchains; 33using namespace clang; 34using namespace llvm::opt; 35 36Windows::Windows(const Driver &D, const llvm::Triple& Triple, 37 const ArgList &Args) 38 : ToolChain(D, Triple, Args) { 39} 40 41Tool *Windows::buildLinker() const { 42 return new tools::visualstudio::Link(*this); 43} 44 45Tool *Windows::buildAssembler() const { 46 if (getTriple().getEnvironment() == llvm::Triple::MachO) 47 return new tools::darwin::Assemble(*this); 48 getDriver().Diag(clang::diag::err_no_external_assembler); 49 return NULL; 50} 51 52bool Windows::IsIntegratedAssemblerDefault() const { 53 return true; 54} 55 56bool Windows::IsUnwindTablesDefault() const { 57 return getArch() == llvm::Triple::x86_64; 58} 59 60bool Windows::isPICDefault() const { 61 return getArch() == llvm::Triple::x86_64; 62} 63 64bool Windows::isPIEDefault() const { 65 return false; 66} 67 68bool Windows::isPICDefaultForced() const { 69 return getArch() == llvm::Triple::x86_64; 70} 71 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. 83static 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; 129 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY, 130 &hTopKey); 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; 139 while (*sp && !isDigit(*sp)) 140 sp++; 141 if (!*sp) 142 continue; 143 const char *ep = sp + 1; 144 while (*ep && (isDigit(*ep) || (*ep == '.'))) 145 ep++; 146 char numBuf[32]; 147 strncpy(numBuf, sp, sizeof(numBuf) - 1); 148 numBuf[sizeof(numBuf) - 1] = '\0'; 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. 153 strcpy(bestName, keyName); 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 } 169 } 170 size = sizeof(keyName) - 1; 171 } 172 RegCloseKey(hTopKey); 173 } 174 } else { 175 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ | KEY_WOW64_32KEY, 176 &hKey); 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. 189static 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 205 // Get Visual Studio installation directory. 206static 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 282void 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)) { 288 SmallString<128> P(getDriver().ResourceDir); 289 llvm::sys::path::append(P, "include"); 290 addSystemInclude(DriverArgs, CC1Args, P.str()); 291 } 292 293 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 294 return; 295 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; 300 StringRef(cl_include_dir).split(Dirs, ";"); 301 int n = 0; 302 for (SmallVectorImpl<StringRef>::iterator I = Dirs.begin(), E = Dirs.end(); 303 I != E; ++I) { 304 StringRef d = *I; 305 if (d.size() == 0) 306 continue; 307 ++n; 308 addSystemInclude(DriverArgs, CC1Args, d); 309 } 310 if (n) return; 311 } 312 313 std::string VSDir; 314 std::string WindowsSDKDir; 315 316 // When built with access to the proper Windows APIs, try to actually find 317 // the correct include paths first. 318 if (getVisualStudioDir(VSDir)) { 319 addSystemInclude(DriverArgs, CC1Args, VSDir + "\\VC\\include"); 320 if (getWindowsSDKDir(WindowsSDKDir)) 321 addSystemInclude(DriverArgs, CC1Args, WindowsSDKDir + "\\include"); 322 else 323 addSystemInclude(DriverArgs, CC1Args, 324 VSDir + "\\VC\\PlatformSDK\\Include"); 325 return; 326 } 327#endif // _MSC_VER 328 329 // As a fallback, select default install paths. 330 const StringRef Paths[] = { 331 "C:/Program Files/Microsoft Visual Studio 10.0/VC/include", 332 "C:/Program Files/Microsoft Visual Studio 9.0/VC/include", 333 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include", 334 "C:/Program Files/Microsoft Visual Studio 8/VC/include", 335 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include" 336 }; 337 addSystemIncludes(DriverArgs, CC1Args, Paths); 338} 339 340void Windows::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 341 ArgStringList &CC1Args) const { 342 // FIXME: There should probably be logic here to find libc++ on Windows. 343} 344