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