InitHeaderSearch.cpp revision 33d838a153f6340ffad044e4fd865289d9fd71f2
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/Support/raw_ostream.h" 25#include "llvm/System/Path.h" 26#include "llvm/Config/config.h" 27#include <cstdio> 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 isysroot; 45 46public: 47 48 InitHeaderSearch(HeaderSearch &HS, 49 bool verbose = false, const std::string &iSysroot = "") 50 : Headers(HS), Verbose(verbose), isysroot(iSysroot) {} 51 52 /// AddPath - Add the specified path to the specified group list. 53 void AddPath(const llvm::StringRef &Path, IncludeDirGroup Group, 54 bool isCXXAware, bool isUserSupplied, 55 bool isFramework, bool IgnoreSysRoot = false); 56 57 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to suport a gnu 58 /// libstdc++. 59 void AddGnuCPlusPlusIncludePaths(const std::string &Base, 60 const char *ArchDir, 61 const char *Dir32, 62 const char *Dir64, 63 const llvm::Triple &triple); 64 65 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW 66 /// libstdc++. 67 void AddMinGWCPlusPlusIncludePaths(const std::string &Base, 68 const char *Arch, 69 const char *Version); 70 71 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH 72 /// separator. The processing follows that of the CPATH variable for gcc. 73 void AddDelimitedPaths(const char *String); 74 75 // AddDefaultCIncludePaths - Add paths that should always be searched. 76 void AddDefaultCIncludePaths(const llvm::Triple &triple); 77 78 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when 79 // compiling c++. 80 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple); 81 82 /// AddDefaultSystemIncludePaths - Adds the default system include paths so 83 /// that e.g. stdio.h is found. 84 void AddDefaultSystemIncludePaths(const LangOptions &Lang, 85 const llvm::Triple &triple); 86 87 /// Realize - Merges all search path lists into one list and send it to 88 /// HeaderSearch. 89 void Realize(); 90}; 91 92} 93 94void InitHeaderSearch::AddPath(const llvm::StringRef &Path, 95 IncludeDirGroup Group, bool isCXXAware, 96 bool isUserSupplied, bool isFramework, 97 bool IgnoreSysRoot) { 98 assert(!Path.empty() && "can't handle empty path here"); 99 FileManager &FM = Headers.getFileMgr(); 100 101 // Compute the actual path, taking into consideration -isysroot. 102 llvm::SmallString<256> MappedPath; 103 104 // Handle isysroot. 105 if (Group == System && !IgnoreSysRoot) { 106 // FIXME: Portability. This should be a sys::Path interface, this doesn't 107 // handle things like C:\ right, nor win32 \\network\device\blah. 108 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present. 109 MappedPath.append(isysroot.begin(), isysroot.end()); 110 } 111 112 MappedPath.append(Path.begin(), Path.end()); 113 114 // Compute the DirectoryLookup type. 115 SrcMgr::CharacteristicKind Type; 116 if (Group == Quoted || Group == Angled) 117 Type = SrcMgr::C_User; 118 else if (isCXXAware) 119 Type = SrcMgr::C_System; 120 else 121 Type = SrcMgr::C_ExternCSystem; 122 123 124 // If the directory exists, add it. 125 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) { 126 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied, 127 isFramework)); 128 return; 129 } 130 131 // Check to see if this is an apple-style headermap (which are not allowed to 132 // be frameworks). 133 if (!isFramework) { 134 if (const FileEntry *FE = FM.getFile(MappedPath.str())) { 135 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) { 136 // It is a headermap, add it to the search path. 137 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied)); 138 return; 139 } 140 } 141 } 142 143 if (Verbose) 144 llvm::errs() << "ignoring nonexistent directory \"" 145 << MappedPath.str() << "\"\n"; 146} 147 148 149void InitHeaderSearch::AddDelimitedPaths(const char *at) { 150 if (*at == 0) // Empty string should not add '.' path. 151 return; 152 153 const char* delim = strchr(at, llvm::sys::PathSeparator); 154 while (delim != 0) { 155 if (delim-at == 0) 156 AddPath(".", Angled, false, true, false); 157 else 158 AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false); 159 at = delim + 1; 160 delim = strchr(at, llvm::sys::PathSeparator); 161 } 162 if (*at == 0) 163 AddPath(".", Angled, false, true, false); 164 else 165 AddPath(at, Angled, false, true, false); 166} 167 168void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(const std::string &Base, 169 const char *ArchDir, 170 const char *Dir32, 171 const char *Dir64, 172 const llvm::Triple &triple) { 173 // Add the common dirs 174 AddPath(Base, System, true, false, false); 175 AddPath(Base + "/backward", System, true, false, false); 176 177 // Add the multilib dirs 178 llvm::Triple::ArchType arch = triple.getArch(); 179 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64; 180 if (is64bit) 181 AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false); 182 else 183 AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false); 184} 185 186void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(const std::string &Base, 187 const char *Arch, 188 const char *Version) { 189 std::string localBase = Base + "/" + Arch + "/" + Version + "/include"; 190 AddPath(localBase, System, true, false, false); 191 AddPath(localBase + "/c++", System, true, false, false); 192 AddPath(localBase + "/c++/backward", System, true, false, false); 193} 194 195 // FIXME: This probably should goto to some platform utils place. 196#ifdef _MSC_VER 197 // Read registry string. 198bool getSystemRegistryString(const char *keyPath, const char *valueName, 199 char *value, size_t maxLength) { 200 HKEY hRootKey = NULL; 201 HKEY hKey = NULL; 202 const char* subKey = NULL; 203 DWORD valueType; 204 DWORD valueSize = maxLength - 1; 205 bool returnValue = false; 206 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) { 207 hRootKey = HKEY_CLASSES_ROOT; 208 subKey = keyPath + 18; 209 } 210 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) { 211 hRootKey = HKEY_USERS; 212 subKey = keyPath + 11; 213 } 214 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) { 215 hRootKey = HKEY_LOCAL_MACHINE; 216 subKey = keyPath + 19; 217 } 218 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) { 219 hRootKey = HKEY_CURRENT_USER; 220 subKey = keyPath + 18; 221 } 222 else 223 return(false); 224 long lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey); 225 if (lResult == ERROR_SUCCESS) { 226 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType, 227 (LPBYTE)value, &valueSize); 228 if (lResult == ERROR_SUCCESS) 229 returnValue = true; 230 RegCloseKey(hKey); 231 } 232 return(returnValue); 233} 234#else // _MSC_VER 235 // Read registry string. 236bool getSystemRegistryString(const char *, const char *, char *, size_t) { 237 return(false); 238} 239#endif // _MSC_VER 240 241 // Get Visual Studio installation directory. 242bool getVisualStudioDir(std::string &path) { 243 // Try the Windows registry first. 244 char vs80IDEInstallDir[256]; 245 char vs90IDEInstallDir[256]; 246 const char* vsIDEInstallDir = NULL; 247 bool has80 = getSystemRegistryString( 248 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0", 249 "InstallDir", vs80IDEInstallDir, sizeof(vs80IDEInstallDir) - 1); 250 bool has90 = getSystemRegistryString( 251 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0", 252 "InstallDir", vs90IDEInstallDir, sizeof(vs90IDEInstallDir) - 1); 253 // If we have both vc80 and vc90, pick version we were compiled with. 254 if (has80 && has90) { 255 #ifdef _MSC_VER 256 #if (_MSC_VER >= 1500) // VC90 257 vsIDEInstallDir = vs90IDEInstallDir; 258 #elif (_MSC_VER == 1400) // VC80 259 vsIDEInstallDir = vs80IDEInstallDir; 260 #else 261 vsIDEInstallDir = vs90IDEInstallDir; 262 #endif 263 #else 264 vsIDEInstallDir = vs90IDEInstallDir; 265 #endif 266 } 267 else if (has90) 268 vsIDEInstallDir = vs90IDEInstallDir; 269 else if (has80) 270 vsIDEInstallDir = vs80IDEInstallDir; 271 if (vsIDEInstallDir && *vsIDEInstallDir) { 272 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE"); 273 if (p) 274 *p = '\0'; 275 path = vsIDEInstallDir; 276 return(true); 277 } 278 else { 279 // Try the environment. 280 const char* vs90comntools = getenv("VS90COMNTOOLS"); 281 const char* vs80comntools = getenv("VS80COMNTOOLS"); 282 const char* vscomntools = NULL; 283 // If we have both vc80 and vc90, pick version we were compiled with. 284 if (vs90comntools && vs80comntools) { 285 #if (_MSC_VER >= 1500) // VC90 286 vscomntools = vs90comntools; 287 #elif (_MSC_VER == 1400) // VC80 288 vscomntools = vs80comntools; 289 #else 290 vscomntools = vs90comntools; 291 #endif 292 } 293 else if (vs90comntools) 294 vscomntools = vs90comntools; 295 else if (vs80comntools) 296 vscomntools = vs80comntools; 297 if (vscomntools && *vscomntools) { 298 char *p = (char*)strstr(vscomntools, "\\Common7\\Tools"); 299 if (p) 300 *p = '\0'; 301 path = vscomntools; 302 return(true); 303 } 304 else 305 return(false); 306 } 307 return(false); 308} 309 310void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple) { 311 // FIXME: temporary hack: hard-coded paths. 312 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS); 313 if (CIncludeDirs != "") { 314 llvm::SmallVector<llvm::StringRef, 5> dirs; 315 CIncludeDirs.split(dirs, ":"); 316 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin(); 317 i != dirs.end(); 318 ++i) 319 AddPath(*i, System, false, false, false); 320 return; 321 } 322 llvm::Triple::OSType os = triple.getOS(); 323 switch (os) { 324 case llvm::Triple::Win32: 325 { 326 std::string VSDir; 327 if (getVisualStudioDir(VSDir)) { 328 AddPath(VSDir + "\\VC\\include", System, false, false, false); 329 AddPath(VSDir + "\\VC\\PlatformSDK\\Include", 330 System, false, false, false); 331 } 332 else { 333 // Default install paths. 334 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include", 335 System, false, false, false); 336 AddPath( 337 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include", 338 System, false, false, false); 339 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include", 340 System, false, false, false); 341 AddPath( 342 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include", 343 System, false, false, false); 344 // For some clang developers. 345 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include", 346 System, false, false, false); 347 AddPath( 348 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include", 349 System, false, false, false); 350 } 351 } 352 break; 353 case llvm::Triple::MinGW64: 354 case llvm::Triple::MinGW32: 355 AddPath("c:/mingw/include", System, true, false, false); 356 break; 357 default: 358 break; 359 } 360 361 AddPath("/usr/local/include", System, false, false, false); 362 AddPath("/usr/include", System, false, false, false); 363} 364 365void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) { 366 llvm::Triple::OSType os = triple.getOS(); 367 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT); 368 if (CxxIncludeRoot != "") { 369 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH); 370 if (CxxIncludeArch == "") 371 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(), 372 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, triple); 373 else 374 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH, 375 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, triple); 376 return; 377 } 378 // FIXME: temporary hack: hard-coded paths. 379 switch (os) { 380 case llvm::Triple::Cygwin: 381 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include", 382 System, true, false, false); 383 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++", 384 System, true, false, false); 385 break; 386 case llvm::Triple::MinGW64: 387 // Try gcc 4.4.0 388 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0"); 389 // Try gcc 4.3.0 390 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0"); 391 // Fall through. 392 case llvm::Triple::MinGW32: 393 // Try gcc 4.4.0 394 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0"); 395 // Try gcc 4.3.0 396 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0"); 397 break; 398 case llvm::Triple::Darwin: 399 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", 400 "i686-apple-darwin10", "", "x86_64", triple); 401 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", 402 "i686-apple-darwin8", "", "", triple); 403 break; 404 case llvm::Triple::Linux: 405 // Ubuntu 7.10 - Gutsy Gibbon 406 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3", 407 "i486-linux-gnu", "", "", triple); 408 // Ubuntu 9.04 409 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3", 410 "x86_64-linux-gnu","32", "", triple); 411 // Ubuntu 9.10 412 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1", 413 "x86_64-linux-gnu", "32", "", triple); 414 // Fedora 8 415 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2", 416 "i386-redhat-linux", "", "", triple); 417 // Fedora 9 418 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0", 419 "i386-redhat-linux", "", "", triple); 420 // Fedora 10 421 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2", 422 "i386-redhat-linux","", "", triple); 423 // openSUSE 11.1 32 bit 424 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 425 "i586-suse-linux", "", "", triple); 426 // openSUSE 11.1 64 bit 427 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 428 "x86_64-suse-linux", "32", "", triple); 429 // openSUSE 11.2 430 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", 431 "i586-suse-linux", "", "", triple); 432 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", 433 "x86_64-suse-linux", "", "", triple); 434 // Arch Linux 2008-06-24 435 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", 436 "i686-pc-linux-gnu", "", "", triple); 437 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", 438 "x86_64-unknown-linux-gnu", "", "", triple); 439 // Gentoo x86 2009.1 stable 440 AddGnuCPlusPlusIncludePaths( 441 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4", 442 "i686-pc-linux-gnu", "", "", triple); 443 // Gentoo x86 2009.0 stable 444 AddGnuCPlusPlusIncludePaths( 445 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4", 446 "i686-pc-linux-gnu", "", "", triple); 447 // Gentoo x86 2008.0 stable 448 AddGnuCPlusPlusIncludePaths( 449 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4", 450 "i686-pc-linux-gnu", "", "", triple); 451 // Ubuntu 8.10 452 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 453 "i486-pc-linux-gnu", "", "", triple); 454 // Ubuntu 9.04 455 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 456 "i486-linux-gnu","", "", triple); 457 // Gentoo amd64 stable 458 AddGnuCPlusPlusIncludePaths( 459 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4", 460 "i686-pc-linux-gnu", "", "", triple); 461 // Exherbo (2009-10-26) 462 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2", 463 "x86_64-pc-linux-gnu", "32", "", triple); 464 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2", 465 "i686-pc-linux-gnu", "", "", triple); 466 break; 467 case llvm::Triple::FreeBSD: 468 // DragonFly 469 AddPath("/usr/include/c++/4.1", System, true, false, false); 470 // FreeBSD 471 AddPath("/usr/include/c++/4.2", System, true, false, false); 472 break; 473 case llvm::Triple::Solaris: 474 // Solaris - Fall though.. 475 case llvm::Triple::AuroraUX: 476 // AuroraUX 477 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4", 478 "i386-pc-solaris2.11", "", "", triple); 479 break; 480 default: 481 break; 482 } 483} 484 485void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang, 486 const llvm::Triple &triple) { 487 AddDefaultCIncludePaths(triple); 488 489 // Add the default framework include paths on Darwin. 490 if (triple.getOS() == llvm::Triple::Darwin) { 491 AddPath("/System/Library/Frameworks", System, true, false, true); 492 AddPath("/Library/Frameworks", System, true, false, true); 493 } 494 495 if (Lang.CPlusPlus) 496 AddDefaultCPlusPlusIncludePaths(triple); 497} 498 499/// RemoveDuplicates - If there are duplicate directory entries in the specified 500/// search list, remove the later (dead) ones. 501static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList, 502 bool Verbose) { 503 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; 504 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; 505 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; 506 for (unsigned i = 0; i != SearchList.size(); ++i) { 507 unsigned DirToRemove = i; 508 509 const DirectoryLookup &CurEntry = SearchList[i]; 510 511 if (CurEntry.isNormalDir()) { 512 // If this isn't the first time we've seen this dir, remove it. 513 if (SeenDirs.insert(CurEntry.getDir())) 514 continue; 515 } else if (CurEntry.isFramework()) { 516 // If this isn't the first time we've seen this framework dir, remove it. 517 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir())) 518 continue; 519 } else { 520 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); 521 // If this isn't the first time we've seen this headermap, remove it. 522 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap())) 523 continue; 524 } 525 526 // If we have a normal #include dir/framework/headermap that is shadowed 527 // later in the chain by a system include location, we actually want to 528 // ignore the user's request and drop the user dir... keeping the system 529 // dir. This is weird, but required to emulate GCC's search path correctly. 530 // 531 // Since dupes of system dirs are rare, just rescan to find the original 532 // that we're nuking instead of using a DenseMap. 533 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) { 534 // Find the dir that this is the same of. 535 unsigned FirstDir; 536 for (FirstDir = 0; ; ++FirstDir) { 537 assert(FirstDir != i && "Didn't find dupe?"); 538 539 const DirectoryLookup &SearchEntry = SearchList[FirstDir]; 540 541 // If these are different lookup types, then they can't be the dupe. 542 if (SearchEntry.getLookupType() != CurEntry.getLookupType()) 543 continue; 544 545 bool isSame; 546 if (CurEntry.isNormalDir()) 547 isSame = SearchEntry.getDir() == CurEntry.getDir(); 548 else if (CurEntry.isFramework()) 549 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir(); 550 else { 551 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); 552 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap(); 553 } 554 555 if (isSame) 556 break; 557 } 558 559 // If the first dir in the search path is a non-system dir, zap it 560 // instead of the system one. 561 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User) 562 DirToRemove = FirstDir; 563 } 564 565 if (Verbose) { 566 fprintf(stderr, "ignoring duplicate directory \"%s\"\n", 567 CurEntry.getName()); 568 if (DirToRemove != i) 569 fprintf(stderr, " as it is a non-system directory that duplicates" 570 " a system directory\n"); 571 } 572 573 // This is reached if the current entry is a duplicate. Remove the 574 // DirToRemove (usually the current dir). 575 SearchList.erase(SearchList.begin()+DirToRemove); 576 --i; 577 } 578} 579 580 581void InitHeaderSearch::Realize() { 582 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. 583 std::vector<DirectoryLookup> SearchList; 584 SearchList = IncludeGroup[Angled]; 585 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(), 586 IncludeGroup[System].end()); 587 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(), 588 IncludeGroup[After].end()); 589 RemoveDuplicates(SearchList, Verbose); 590 RemoveDuplicates(IncludeGroup[Quoted], Verbose); 591 592 // Prepend QUOTED list on the search list. 593 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(), 594 IncludeGroup[Quoted].end()); 595 596 597 bool DontSearchCurDir = false; // TODO: set to true if -I- is set? 598 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(), 599 DontSearchCurDir); 600 601 // If verbose, print the list of directories that will be searched. 602 if (Verbose) { 603 fprintf(stderr, "#include \"...\" search starts here:\n"); 604 unsigned QuotedIdx = IncludeGroup[Quoted].size(); 605 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { 606 if (i == QuotedIdx) 607 fprintf(stderr, "#include <...> search starts here:\n"); 608 const char *Name = SearchList[i].getName(); 609 const char *Suffix; 610 if (SearchList[i].isNormalDir()) 611 Suffix = ""; 612 else if (SearchList[i].isFramework()) 613 Suffix = " (framework directory)"; 614 else { 615 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup"); 616 Suffix = " (headermap)"; 617 } 618 fprintf(stderr, " %s%s\n", Name, Suffix); 619 } 620 fprintf(stderr, "End of search list.\n"); 621 } 622} 623 624void clang::ApplyHeaderSearchOptions(HeaderSearch &HS, 625 const HeaderSearchOptions &HSOpts, 626 const LangOptions &Lang, 627 const llvm::Triple &Triple) { 628 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot); 629 630 // Add the user defined entries. 631 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) { 632 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i]; 633 Init.AddPath(E.Path, E.Group, E.IsCXXAware, E.IsUserSupplied, E.IsFramework, 634 E.IgnoreSysRoot); 635 } 636 637 // Add entries from CPATH and friends. 638 Init.AddDelimitedPaths(HSOpts.EnvIncPath.c_str()); 639 Init.AddDelimitedPaths(HSOpts.LangEnvIncPath.c_str()); 640 641 if (!HSOpts.BuiltinIncludePath.empty()) { 642 // Ignore the sys root, we *always* look for clang headers relative to 643 // supplied path. 644 Init.AddPath(HSOpts.BuiltinIncludePath, System, 645 false, false, false, /*IgnoreSysRoot=*/ true); 646 } 647 648 if (HSOpts.UseStandardIncludes) 649 Init.AddDefaultSystemIncludePaths(Lang, Triple); 650 651 Init.Realize(); 652} 653