InitHeaderSearch.cpp revision af6530c938bfc60902f0dfec1c0808aedbee1663
1//===--- InitHeaderSearch.cpp - Initialize header search paths ------------===// 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// This file implements the InitHeaderSearch class. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Frontend/Utils.h" 15#include "clang/Basic/FileManager.h" 16#include "clang/Basic/LangOptions.h" 17#include "clang/Frontend/HeaderSearchOptions.h" 18#include "clang/Lex/HeaderSearch.h" 19#include "llvm/ADT/SmallString.h" 20#include "llvm/ADT/SmallPtrSet.h" 21#include "llvm/ADT/SmallVector.h" 22#include "llvm/ADT/StringExtras.h" 23#include "llvm/ADT/Triple.h" 24#include "llvm/ADT/Twine.h" 25#include "llvm/Support/raw_ostream.h" 26#include "llvm/Support/Path.h" 27#include "llvm/Config/config.h" 28#ifdef _MSC_VER 29 #define WIN32_LEAN_AND_MEAN 1 30 #include <windows.h> 31#endif 32using namespace clang; 33using namespace clang::frontend; 34 35namespace { 36 37/// InitHeaderSearch - This class makes it easier to set the search paths of 38/// a HeaderSearch object. InitHeaderSearch stores several search path lists 39/// internally, which can be sent to a HeaderSearch object in one swoop. 40class InitHeaderSearch { 41 std::vector<DirectoryLookup> IncludeGroup[4]; 42 HeaderSearch& Headers; 43 bool Verbose; 44 std::string IncludeSysroot; 45 bool IsNotEmptyOrRoot; 46 47public: 48 49 InitHeaderSearch(HeaderSearch &HS, bool verbose, llvm::StringRef sysroot) 50 : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot), 51 IsNotEmptyOrRoot(!(sysroot.empty() || sysroot == "/")) { 52 } 53 54 /// AddPath - Add the specified path to the specified group list. 55 void AddPath(const llvm::Twine &Path, IncludeDirGroup Group, 56 bool isCXXAware, bool isUserSupplied, 57 bool isFramework, bool IgnoreSysRoot = false); 58 59 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu 60 /// libstdc++. 61 void AddGnuCPlusPlusIncludePaths(llvm::StringRef Base, 62 llvm::StringRef ArchDir, 63 llvm::StringRef Dir32, 64 llvm::StringRef Dir64, 65 const llvm::Triple &triple); 66 67 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW 68 /// libstdc++. 69 void AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base, 70 llvm::StringRef Arch, 71 llvm::StringRef Version); 72 73 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH 74 /// separator. The processing follows that of the CPATH variable for gcc. 75 void AddDelimitedPaths(llvm::StringRef String); 76 77 // AddDefaultCIncludePaths - Add paths that should always be searched. 78 void AddDefaultCIncludePaths(const llvm::Triple &triple, 79 const HeaderSearchOptions &HSOpts); 80 81 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when 82 // compiling c++. 83 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple); 84 85 /// AddDefaultSystemIncludePaths - Adds the default system include paths so 86 /// that e.g. stdio.h is found. 87 void AddDefaultSystemIncludePaths(const LangOptions &Lang, 88 const llvm::Triple &triple, 89 const HeaderSearchOptions &HSOpts); 90 91 /// Realize - Merges all search path lists into one list and send it to 92 /// HeaderSearch. 93 void Realize(); 94}; 95 96} 97 98void InitHeaderSearch::AddPath(const llvm::Twine &Path, 99 IncludeDirGroup Group, bool isCXXAware, 100 bool isUserSupplied, bool isFramework, 101 bool IgnoreSysRoot) { 102 assert(!Path.isTriviallyEmpty() && "can't handle empty path here"); 103 FileManager &FM = Headers.getFileMgr(); 104 105 // Compute the actual path, taking into consideration -isysroot. 106 llvm::SmallString<256> MappedPathStorage; 107 llvm::StringRef MappedPathStr = Path.toStringRef(MappedPathStorage); 108 109 // Handle isysroot. 110 if (Group == System && !IgnoreSysRoot && 111 llvm::sys::path::is_absolute(MappedPathStr) && 112 IsNotEmptyOrRoot) { 113 MappedPathStorage.clear(); 114 MappedPathStr = 115 (IncludeSysroot + Path).toStringRef(MappedPathStorage); 116 } 117 118 // Compute the DirectoryLookup type. 119 SrcMgr::CharacteristicKind Type; 120 if (Group == Quoted || Group == Angled) 121 Type = SrcMgr::C_User; 122 else if (isCXXAware) 123 Type = SrcMgr::C_System; 124 else 125 Type = SrcMgr::C_ExternCSystem; 126 127 128 // If the directory exists, add it. 129 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) { 130 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied, 131 isFramework)); 132 return; 133 } 134 135 // Check to see if this is an apple-style headermap (which are not allowed to 136 // be frameworks). 137 if (!isFramework) { 138 if (const FileEntry *FE = FM.getFile(MappedPathStr)) { 139 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) { 140 // It is a headermap, add it to the search path. 141 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied)); 142 return; 143 } 144 } 145 } 146 147 if (Verbose) 148 llvm::errs() << "ignoring nonexistent directory \"" 149 << MappedPathStr << "\"\n"; 150} 151 152 153void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) { 154 if (at.empty()) // Empty string should not add '.' path. 155 return; 156 157 llvm::StringRef::size_type delim; 158 while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) { 159 if (delim == 0) 160 AddPath(".", Angled, false, true, false); 161 else 162 AddPath(at.substr(0, delim), Angled, false, true, false); 163 at = at.substr(delim + 1); 164 } 165 166 if (at.empty()) 167 AddPath(".", Angled, false, true, false); 168 else 169 AddPath(at, Angled, false, true, false); 170} 171 172void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base, 173 llvm::StringRef ArchDir, 174 llvm::StringRef Dir32, 175 llvm::StringRef Dir64, 176 const llvm::Triple &triple) { 177 // Add the base dir 178 AddPath(Base, System, true, false, false); 179 180 // Add the multilib dirs 181 llvm::Triple::ArchType arch = triple.getArch(); 182 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64; 183 if (is64bit) 184 AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false); 185 else 186 AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false); 187 188 // Add the backward dir 189 AddPath(Base + "/backward", System, true, false, false); 190} 191 192void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base, 193 llvm::StringRef Arch, 194 llvm::StringRef Version) { 195 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++", 196 System, true, false, false); 197 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch, 198 System, true, false, false); 199 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward", 200 System, true, false, false); 201} 202 203 // FIXME: This probably should goto to some platform utils place. 204#ifdef _MSC_VER 205 206 // Read registry string. 207 // This also supports a means to look for high-versioned keys by use 208 // of a $VERSION placeholder in the key path. 209 // $VERSION in the key path is a placeholder for the version number, 210 // causing the highest value path to be searched for and used. 211 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION". 212 // There can be additional characters in the component. Only the numberic 213 // characters are compared. 214static bool getSystemRegistryString(const char *keyPath, const char *valueName, 215 char *value, size_t maxLength) { 216 HKEY hRootKey = NULL; 217 HKEY hKey = NULL; 218 const char* subKey = NULL; 219 DWORD valueType; 220 DWORD valueSize = maxLength - 1; 221 long lResult; 222 bool returnValue = false; 223 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) { 224 hRootKey = HKEY_CLASSES_ROOT; 225 subKey = keyPath + 18; 226 } 227 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) { 228 hRootKey = HKEY_USERS; 229 subKey = keyPath + 11; 230 } 231 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) { 232 hRootKey = HKEY_LOCAL_MACHINE; 233 subKey = keyPath + 19; 234 } 235 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) { 236 hRootKey = HKEY_CURRENT_USER; 237 subKey = keyPath + 18; 238 } 239 else 240 return(false); 241 const char *placeHolder = strstr(subKey, "$VERSION"); 242 char bestName[256]; 243 bestName[0] = '\0'; 244 // If we have a $VERSION placeholder, do the highest-version search. 245 if (placeHolder) { 246 const char *keyEnd = placeHolder - 1; 247 const char *nextKey = placeHolder; 248 // Find end of previous key. 249 while ((keyEnd > subKey) && (*keyEnd != '\\')) 250 keyEnd--; 251 // Find end of key containing $VERSION. 252 while (*nextKey && (*nextKey != '\\')) 253 nextKey++; 254 size_t partialKeyLength = keyEnd - subKey; 255 char partialKey[256]; 256 if (partialKeyLength > sizeof(partialKey)) 257 partialKeyLength = sizeof(partialKey); 258 strncpy(partialKey, subKey, partialKeyLength); 259 partialKey[partialKeyLength] = '\0'; 260 HKEY hTopKey = NULL; 261 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey); 262 if (lResult == ERROR_SUCCESS) { 263 char keyName[256]; 264 int bestIndex = -1; 265 double bestValue = 0.0; 266 DWORD index, size = sizeof(keyName) - 1; 267 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL, 268 NULL, NULL, NULL) == ERROR_SUCCESS; index++) { 269 const char *sp = keyName; 270 while (*sp && !isdigit(*sp)) 271 sp++; 272 if (!*sp) 273 continue; 274 const char *ep = sp + 1; 275 while (*ep && (isdigit(*ep) || (*ep == '.'))) 276 ep++; 277 char numBuf[32]; 278 strncpy(numBuf, sp, sizeof(numBuf) - 1); 279 numBuf[sizeof(numBuf) - 1] = '\0'; 280 double value = strtod(numBuf, NULL); 281 if (value > bestValue) { 282 bestIndex = (int)index; 283 bestValue = value; 284 strcpy(bestName, keyName); 285 } 286 size = sizeof(keyName) - 1; 287 } 288 // If we found the highest versioned key, open the key and get the value. 289 if (bestIndex != -1) { 290 // Append rest of key. 291 strncat(bestName, nextKey, sizeof(bestName) - 1); 292 bestName[sizeof(bestName) - 1] = '\0'; 293 // Open the chosen key path remainder. 294 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey); 295 if (lResult == ERROR_SUCCESS) { 296 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType, 297 (LPBYTE)value, &valueSize); 298 if (lResult == ERROR_SUCCESS) 299 returnValue = true; 300 RegCloseKey(hKey); 301 } 302 } 303 RegCloseKey(hTopKey); 304 } 305 } 306 else { 307 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey); 308 if (lResult == ERROR_SUCCESS) { 309 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType, 310 (LPBYTE)value, &valueSize); 311 if (lResult == ERROR_SUCCESS) 312 returnValue = true; 313 RegCloseKey(hKey); 314 } 315 } 316 return(returnValue); 317} 318#else // _MSC_VER 319 // Read registry string. 320static bool getSystemRegistryString(const char*, const char*, char*, size_t) { 321 return(false); 322} 323#endif // _MSC_VER 324 325 // Get Visual Studio installation directory. 326static bool getVisualStudioDir(std::string &path) { 327 // First check the environment variables that vsvars32.bat sets. 328 const char* vcinstalldir = getenv("VCINSTALLDIR"); 329 if(vcinstalldir) { 330 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC")); 331 if (p) 332 *p = '\0'; 333 path = vcinstalldir; 334 return(true); 335 } 336 337 char vsIDEInstallDir[256]; 338 char vsExpressIDEInstallDir[256]; 339 // Then try the windows registry. 340 bool hasVCDir = getSystemRegistryString( 341 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION", 342 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1); 343 bool hasVCExpressDir = getSystemRegistryString( 344 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION", 345 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1); 346 // If we have both vc80 and vc90, pick version we were compiled with. 347 if (hasVCDir && vsIDEInstallDir[0]) { 348 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE"); 349 if (p) 350 *p = '\0'; 351 path = vsIDEInstallDir; 352 return(true); 353 } 354 else if (hasVCExpressDir && vsExpressIDEInstallDir[0]) { 355 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE"); 356 if (p) 357 *p = '\0'; 358 path = vsExpressIDEInstallDir; 359 return(true); 360 } 361 else { 362 // Try the environment. 363 const char* vs100comntools = getenv("VS100COMNTOOLS"); 364 const char* vs90comntools = getenv("VS90COMNTOOLS"); 365 const char* vs80comntools = getenv("VS80COMNTOOLS"); 366 const char* vscomntools = NULL; 367 368 // Try to find the version that we were compiled with 369 if(false) {} 370 #if (_MSC_VER >= 1600) // VC100 371 else if(vs100comntools) { 372 vscomntools = vs100comntools; 373 } 374 #elif (_MSC_VER == 1500) // VC80 375 else if(vs90comntools) { 376 vscomntools = vs90comntools; 377 } 378 #elif (_MSC_VER == 1400) // VC80 379 else if(vs80comntools) { 380 vscomntools = vs80comntools; 381 } 382 #endif 383 // Otherwise find any version we can 384 else if (vs100comntools) 385 vscomntools = vs100comntools; 386 else if (vs90comntools) 387 vscomntools = vs90comntools; 388 else if (vs80comntools) 389 vscomntools = vs80comntools; 390 391 if (vscomntools && *vscomntools) { 392 char *p = const_cast<char *>(strstr(vscomntools, "\\Common7\\Tools")); 393 if (p) 394 *p = '\0'; 395 path = vscomntools; 396 return(true); 397 } 398 else 399 return(false); 400 } 401 return(false); 402} 403 404 // Get Windows SDK installation directory. 405static bool getWindowsSDKDir(std::string &path) { 406 char windowsSDKInstallDir[256]; 407 // Try the Windows registry. 408 bool hasSDKDir = getSystemRegistryString( 409 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION", 410 "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1); 411 // If we have both vc80 and vc90, pick version we were compiled with. 412 if (hasSDKDir && windowsSDKInstallDir[0]) { 413 path = windowsSDKInstallDir; 414 return(true); 415 } 416 return(false); 417} 418 419void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple, 420 const HeaderSearchOptions &HSOpts) { 421 // FIXME: temporary hack: hard-coded paths. 422 AddPath("/usr/local/include", System, true, false, false); 423 424 // Builtin includes use #include_next directives and should be positioned 425 // just prior C include dirs. 426 if (HSOpts.UseBuiltinIncludes) { 427 // Ignore the sys root, we *always* look for clang headers relative to 428 // supplied path. 429 llvm::sys::Path P(HSOpts.ResourceDir); 430 P.appendComponent("include"); 431 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true); 432 } 433 434 // Add dirs specified via 'configure --with-c-include-dirs'. 435 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS); 436 if (CIncludeDirs != "") { 437 llvm::SmallVector<llvm::StringRef, 5> dirs; 438 CIncludeDirs.split(dirs, ":"); 439 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin(); 440 i != dirs.end(); 441 ++i) 442 AddPath(*i, System, false, false, false); 443 return; 444 } 445 llvm::Triple::OSType os = triple.getOS(); 446 switch (os) { 447 case llvm::Triple::Win32: { 448 std::string VSDir; 449 std::string WindowsSDKDir; 450 if (getVisualStudioDir(VSDir)) { 451 AddPath(VSDir + "\\VC\\include", System, false, false, false); 452 if (getWindowsSDKDir(WindowsSDKDir)) 453 AddPath(WindowsSDKDir + "\\include", System, false, false, false); 454 else 455 AddPath(VSDir + "\\VC\\PlatformSDK\\Include", 456 System, false, false, false); 457 } else { 458 // Default install paths. 459 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include", 460 System, false, false, false); 461 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include", 462 System, false, false, false); 463 AddPath( 464 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include", 465 System, false, false, false); 466 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include", 467 System, false, false, false); 468 AddPath( 469 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include", 470 System, false, false, false); 471 // For some clang developers. 472 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include", 473 System, false, false, false); 474 AddPath( 475 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include", 476 System, false, false, false); 477 } 478 break; 479 } 480 case llvm::Triple::Haiku: 481 AddPath("/boot/common/include", System, true, false, false); 482 AddPath("/boot/develop/headers/os", System, true, false, false); 483 AddPath("/boot/develop/headers/os/app", System, true, false, false); 484 AddPath("/boot/develop/headers/os/arch", System, true, false, false); 485 AddPath("/boot/develop/headers/os/device", System, true, false, false); 486 AddPath("/boot/develop/headers/os/drivers", System, true, false, false); 487 AddPath("/boot/develop/headers/os/game", System, true, false, false); 488 AddPath("/boot/develop/headers/os/interface", System, true, false, false); 489 AddPath("/boot/develop/headers/os/kernel", System, true, false, false); 490 AddPath("/boot/develop/headers/os/locale", System, true, false, false); 491 AddPath("/boot/develop/headers/os/mail", System, true, false, false); 492 AddPath("/boot/develop/headers/os/media", System, true, false, false); 493 AddPath("/boot/develop/headers/os/midi", System, true, false, false); 494 AddPath("/boot/develop/headers/os/midi2", System, true, false, false); 495 AddPath("/boot/develop/headers/os/net", System, true, false, false); 496 AddPath("/boot/develop/headers/os/storage", System, true, false, false); 497 AddPath("/boot/develop/headers/os/support", System, true, false, false); 498 AddPath("/boot/develop/headers/os/translation", 499 System, true, false, false); 500 AddPath("/boot/develop/headers/os/add-ons/graphics", 501 System, true, false, false); 502 AddPath("/boot/develop/headers/os/add-ons/input_server", 503 System, true, false, false); 504 AddPath("/boot/develop/headers/os/add-ons/screen_saver", 505 System, true, false, false); 506 AddPath("/boot/develop/headers/os/add-ons/tracker", 507 System, true, false, false); 508 AddPath("/boot/develop/headers/os/be_apps/Deskbar", 509 System, true, false, false); 510 AddPath("/boot/develop/headers/os/be_apps/NetPositive", 511 System, true, false, false); 512 AddPath("/boot/develop/headers/os/be_apps/Tracker", 513 System, true, false, false); 514 AddPath("/boot/develop/headers/cpp", System, true, false, false); 515 AddPath("/boot/develop/headers/cpp/i586-pc-haiku", 516 System, true, false, false); 517 AddPath("/boot/develop/headers/3rdparty", System, true, false, false); 518 AddPath("/boot/develop/headers/bsd", System, true, false, false); 519 AddPath("/boot/develop/headers/glibc", System, true, false, false); 520 AddPath("/boot/develop/headers/posix", System, true, false, false); 521 AddPath("/boot/develop/headers", System, true, false, false); 522 break; 523 case llvm::Triple::Cygwin: 524 AddPath("/usr/include/w32api", System, true, false, false); 525 break; 526 case llvm::Triple::MinGW64: 527 case llvm::Triple::MinGW32: 528 AddPath("c:/mingw/include", System, true, false, false); 529 break; 530 default: 531 break; 532 } 533 534 AddPath("/usr/include", System, false, false, false); 535} 536 537void InitHeaderSearch:: 538AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) { 539 llvm::Triple::OSType os = triple.getOS(); 540 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT); 541 if (CxxIncludeRoot != "") { 542 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH); 543 if (CxxIncludeArch == "") 544 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(), 545 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, 546 triple); 547 else 548 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH, 549 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, 550 triple); 551 return; 552 } 553 // FIXME: temporary hack: hard-coded paths. 554 switch (os) { 555 case llvm::Triple::Cygwin: 556 // Cygwin-1.7 557 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4"); 558 // g++-4 / Cygwin-1.5 559 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2"); 560 // FIXME: Do we support g++-3.4.4? 561 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4"); 562 break; 563 case llvm::Triple::MinGW64: 564 // Try gcc 4.5.0 565 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.5.0"); 566 // Try gcc 4.4.0 567 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0"); 568 // Try gcc 4.3.0 569 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0"); 570 // Fall through. 571 case llvm::Triple::MinGW32: 572 // Try gcc 4.5.0 573 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0"); 574 // Try gcc 4.4.0 575 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0"); 576 // Try gcc 4.3.0 577 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0"); 578 break; 579 case llvm::Triple::Darwin: 580 switch (triple.getArch()) { 581 default: break; 582 583 case llvm::Triple::ppc: 584 case llvm::Triple::ppc64: 585 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 586 "powerpc-apple-darwin10", "", "ppc64", 587 triple); 588 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", 589 "powerpc-apple-darwin10", "", "ppc64", 590 triple); 591 break; 592 593 case llvm::Triple::x86: 594 case llvm::Triple::x86_64: 595 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 596 "i686-apple-darwin10", "", "x86_64", triple); 597 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", 598 "i686-apple-darwin8", "", "", triple); 599 break; 600 601 case llvm::Triple::arm: 602 case llvm::Triple::thumb: 603 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 604 "arm-apple-darwin10", "v7", "", triple); 605 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 606 "arm-apple-darwin10", "v6", "", triple); 607 break; 608 } 609 break; 610 case llvm::Triple::DragonFly: 611 AddPath("/usr/include/c++/4.1", System, true, false, false); 612 break; 613 case llvm::Triple::Linux: 614 //===------------------------------------------------------------------===// 615 // Debian based distros. 616 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y 617 //===------------------------------------------------------------------===// 618 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3 619 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1 620 // Debian 6.0 "squeeze" -- gcc-4.4.2 621 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", 622 "x86_64-linux-gnu", "32", "", triple); 623 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", 624 "i486-linux-gnu", "", "64", triple); 625 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3 626 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2 627 // Debian 5.0 "lenny" -- gcc-4.3.2 628 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 629 "x86_64-linux-gnu", "32", "", triple); 630 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 631 "i486-linux-gnu", "", "64", triple); 632 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 633 "arm-linux-gnueabi", "", "", triple); 634 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4 635 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3 636 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", 637 "x86_64-linux-gnu", "32", "", triple); 638 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", 639 "i486-linux-gnu", "", "64", triple); 640 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3 641 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1", 642 "x86_64-linux-gnu", "32", "", triple); 643 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1", 644 "i486-linux-gnu", "", "64", triple); 645 646 //===------------------------------------------------------------------===// 647 // Redhat based distros. 648 //===------------------------------------------------------------------===// 649 // Fedora 14 650 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1", 651 "x86_64-redhat-linux", "32", "", triple); 652 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1", 653 "i686-redhat-linux", "", "", triple); 654 // Fedora 13 655 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4", 656 "x86_64-redhat-linux", "32", "", triple); 657 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4", 658 "i686-redhat-linux","", "", triple); 659 // Fedora 12 660 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3", 661 "x86_64-redhat-linux", "32", "", triple); 662 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3", 663 "i686-redhat-linux","", "", triple); 664 // Fedora 12 (pre-FEB-2010) 665 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2", 666 "x86_64-redhat-linux", "32", "", triple); 667 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2", 668 "i686-redhat-linux","", "", triple); 669 // Fedora 11 670 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1", 671 "x86_64-redhat-linux", "32", "", triple); 672 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1", 673 "i586-redhat-linux","", "", triple); 674 // Fedora 10 675 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2", 676 "x86_64-redhat-linux", "32", "", triple); 677 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2", 678 "i386-redhat-linux","", "", triple); 679 // Fedora 9 680 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0", 681 "x86_64-redhat-linux", "32", "", triple); 682 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0", 683 "i386-redhat-linux", "", "", triple); 684 // Fedora 8 685 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2", 686 "x86_64-redhat-linux", "", "", triple); 687 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2", 688 "i386-redhat-linux", "", "", triple); 689 690 //===------------------------------------------------------------------===// 691 692 // Exherbo (2010-01-25) 693 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3", 694 "x86_64-pc-linux-gnu", "32", "", triple); 695 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3", 696 "i686-pc-linux-gnu", "", "", triple); 697 698 // openSUSE 11.1 32 bit 699 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 700 "i586-suse-linux", "", "", triple); 701 // openSUSE 11.1 64 bit 702 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 703 "x86_64-suse-linux", "32", "", triple); 704 // openSUSE 11.2 705 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", 706 "i586-suse-linux", "", "", triple); 707 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", 708 "x86_64-suse-linux", "", "", triple); 709 710 // openSUSE 11.4 711 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5", 712 "i586-suse-linux", "", "", triple); 713 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5", 714 "x86_64-suse-linux", "", "", triple); 715 716 // Arch Linux 2008-06-24 717 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", 718 "i686-pc-linux-gnu", "", "", triple); 719 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", 720 "x86_64-unknown-linux-gnu", "", "", triple); 721 // Gentoo x86 2010.0 stable 722 AddGnuCPlusPlusIncludePaths( 723 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4", 724 "i686-pc-linux-gnu", "", "", triple); 725 // Gentoo x86 2009.1 stable 726 AddGnuCPlusPlusIncludePaths( 727 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4", 728 "i686-pc-linux-gnu", "", "", triple); 729 // Gentoo x86 2009.0 stable 730 AddGnuCPlusPlusIncludePaths( 731 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4", 732 "i686-pc-linux-gnu", "", "", triple); 733 // Gentoo x86 2008.0 stable 734 AddGnuCPlusPlusIncludePaths( 735 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4", 736 "i686-pc-linux-gnu", "", "", triple); 737 738 // Gentoo amd64 gcc 4.4.5 739 AddGnuCPlusPlusIncludePaths( 740 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4", 741 "x86_64-pc-linux-gnu", "32", "", triple); 742 // Gentoo amd64 gcc 4.4.4 743 AddGnuCPlusPlusIncludePaths( 744 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4", 745 "x86_64-pc-linux-gnu", "32", "", triple); 746 // Gentoo amd64 gcc 4.4.3 747 AddGnuCPlusPlusIncludePaths( 748 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4", 749 "x86_64-pc-linux-gnu", "32", "", triple); 750 // Gentoo amd64 gcc 4.3.2 751 AddGnuCPlusPlusIncludePaths( 752 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4", 753 "x86_64-pc-linux-gnu", "", "", triple); 754 // Gentoo amd64 stable 755 AddGnuCPlusPlusIncludePaths( 756 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4", 757 "i686-pc-linux-gnu", "", "", triple); 758 759 // Gentoo amd64 llvm-gcc trunk 760 AddGnuCPlusPlusIncludePaths( 761 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1", 762 "x86_64-pc-linux-gnu", "", "", triple); 763 764 break; 765 case llvm::Triple::FreeBSD: 766 // FreeBSD 8.0 767 // FreeBSD 7.3 768 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple); 769 break; 770 case llvm::Triple::NetBSD: 771 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple); 772 break; 773 case llvm::Triple::OpenBSD: { 774 std::string t = triple.getTriple(); 775 if (t.substr(0, 6) == "x86_64") 776 t.replace(0, 6, "amd64"); 777 AddGnuCPlusPlusIncludePaths("/usr/include/g++", 778 t, "", "", triple); 779 break; 780 } 781 case llvm::Triple::Minix: 782 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3", 783 "", "", "", triple); 784 break; 785 case llvm::Triple::Solaris: 786 // Solaris - Fall though.. 787 case llvm::Triple::AuroraUX: 788 // AuroraUX 789 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4", 790 "i386-pc-solaris2.11", "", "", triple); 791 break; 792 default: 793 break; 794 } 795} 796 797void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang, 798 const llvm::Triple &triple, 799 const HeaderSearchOptions &HSOpts) { 800 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes) { 801 if (!HSOpts.CXXSystemIncludes.empty()) { 802 for (unsigned i = 0, e = HSOpts.CXXSystemIncludes.size(); i != e; ++i) 803 AddPath(HSOpts.CXXSystemIncludes[i], System, true, false, false); 804 } else 805 AddDefaultCPlusPlusIncludePaths(triple); 806 } 807 808 AddDefaultCIncludePaths(triple, HSOpts); 809 810 // Add the default framework include paths on Darwin. 811 if (triple.getOS() == llvm::Triple::Darwin) { 812 AddPath("/System/Library/Frameworks", System, true, false, true); 813 AddPath("/Library/Frameworks", System, true, false, true); 814 } 815} 816 817/// RemoveDuplicates - If there are duplicate directory entries in the specified 818/// search list, remove the later (dead) ones. 819static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList, 820 bool Verbose) { 821 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; 822 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; 823 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; 824 for (unsigned i = 0; i != SearchList.size(); ++i) { 825 unsigned DirToRemove = i; 826 827 const DirectoryLookup &CurEntry = SearchList[i]; 828 829 if (CurEntry.isNormalDir()) { 830 // If this isn't the first time we've seen this dir, remove it. 831 if (SeenDirs.insert(CurEntry.getDir())) 832 continue; 833 } else if (CurEntry.isFramework()) { 834 // If this isn't the first time we've seen this framework dir, remove it. 835 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir())) 836 continue; 837 } else { 838 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); 839 // If this isn't the first time we've seen this headermap, remove it. 840 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap())) 841 continue; 842 } 843 844 // If we have a normal #include dir/framework/headermap that is shadowed 845 // later in the chain by a system include location, we actually want to 846 // ignore the user's request and drop the user dir... keeping the system 847 // dir. This is weird, but required to emulate GCC's search path correctly. 848 // 849 // Since dupes of system dirs are rare, just rescan to find the original 850 // that we're nuking instead of using a DenseMap. 851 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) { 852 // Find the dir that this is the same of. 853 unsigned FirstDir; 854 for (FirstDir = 0; ; ++FirstDir) { 855 assert(FirstDir != i && "Didn't find dupe?"); 856 857 const DirectoryLookup &SearchEntry = SearchList[FirstDir]; 858 859 // If these are different lookup types, then they can't be the dupe. 860 if (SearchEntry.getLookupType() != CurEntry.getLookupType()) 861 continue; 862 863 bool isSame; 864 if (CurEntry.isNormalDir()) 865 isSame = SearchEntry.getDir() == CurEntry.getDir(); 866 else if (CurEntry.isFramework()) 867 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir(); 868 else { 869 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); 870 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap(); 871 } 872 873 if (isSame) 874 break; 875 } 876 877 // If the first dir in the search path is a non-system dir, zap it 878 // instead of the system one. 879 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User) 880 DirToRemove = FirstDir; 881 } 882 883 if (Verbose) { 884 llvm::errs() << "ignoring duplicate directory \"" 885 << CurEntry.getName() << "\"\n"; 886 if (DirToRemove != i) 887 llvm::errs() << " as it is a non-system directory that duplicates " 888 << "a system directory\n"; 889 } 890 891 // This is reached if the current entry is a duplicate. Remove the 892 // DirToRemove (usually the current dir). 893 SearchList.erase(SearchList.begin()+DirToRemove); 894 --i; 895 } 896} 897 898 899void InitHeaderSearch::Realize() { 900 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. 901 std::vector<DirectoryLookup> SearchList; 902 SearchList = IncludeGroup[Angled]; 903 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(), 904 IncludeGroup[System].end()); 905 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(), 906 IncludeGroup[After].end()); 907 RemoveDuplicates(SearchList, Verbose); 908 RemoveDuplicates(IncludeGroup[Quoted], Verbose); 909 910 // Prepend QUOTED list on the search list. 911 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(), 912 IncludeGroup[Quoted].end()); 913 914 915 bool DontSearchCurDir = false; // TODO: set to true if -I- is set? 916 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(), 917 DontSearchCurDir); 918 919 // If verbose, print the list of directories that will be searched. 920 if (Verbose) { 921 llvm::errs() << "#include \"...\" search starts here:\n"; 922 unsigned QuotedIdx = IncludeGroup[Quoted].size(); 923 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { 924 if (i == QuotedIdx) 925 llvm::errs() << "#include <...> search starts here:\n"; 926 const char *Name = SearchList[i].getName(); 927 const char *Suffix; 928 if (SearchList[i].isNormalDir()) 929 Suffix = ""; 930 else if (SearchList[i].isFramework()) 931 Suffix = " (framework directory)"; 932 else { 933 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup"); 934 Suffix = " (headermap)"; 935 } 936 llvm::errs() << " " << Name << Suffix << "\n"; 937 } 938 llvm::errs() << "End of search list.\n"; 939 } 940} 941 942void clang::ApplyHeaderSearchOptions(HeaderSearch &HS, 943 const HeaderSearchOptions &HSOpts, 944 const LangOptions &Lang, 945 const llvm::Triple &Triple) { 946 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot); 947 948 // Add the user defined entries. 949 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) { 950 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i]; 951 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework, 952 !E.IsSysRootRelative); 953 } 954 955 // Add entries from CPATH and friends. 956 Init.AddDelimitedPaths(HSOpts.EnvIncPath); 957 if (Lang.CPlusPlus && Lang.ObjC1) 958 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath); 959 else if (Lang.CPlusPlus) 960 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath); 961 else if (Lang.ObjC1) 962 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath); 963 else 964 Init.AddDelimitedPaths(HSOpts.CEnvIncPath); 965 966 if (HSOpts.UseStandardIncludes) 967 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts); 968 969 Init.Realize(); 970} 971