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