InitHeaderSearch.cpp revision 33cc2437ae3a609cdc44179931a2909eb48a8200
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 // Fedora 12 503 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2", 504 "i686-redhat-linux","", "", triple); 505 506 // openSUSE 11.1 32 bit 507 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 508 "i586-suse-linux", "", "", triple); 509 // openSUSE 11.1 64 bit 510 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 511 "x86_64-suse-linux", "32", "", triple); 512 // openSUSE 11.2 513 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", 514 "i586-suse-linux", "", "", triple); 515 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", 516 "x86_64-suse-linux", "", "", triple); 517 // Arch Linux 2008-06-24 518 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", 519 "i686-pc-linux-gnu", "", "", triple); 520 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", 521 "x86_64-unknown-linux-gnu", "", "", triple); 522 // Gentoo x86 2009.1 stable 523 AddGnuCPlusPlusIncludePaths( 524 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4", 525 "i686-pc-linux-gnu", "", "", triple); 526 // Gentoo x86 2009.0 stable 527 AddGnuCPlusPlusIncludePaths( 528 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4", 529 "i686-pc-linux-gnu", "", "", triple); 530 // Gentoo x86 2008.0 stable 531 AddGnuCPlusPlusIncludePaths( 532 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4", 533 "i686-pc-linux-gnu", "", "", triple); 534 // Ubuntu 8.10 535 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 536 "i486-pc-linux-gnu", "", "", triple); 537 // Ubuntu 9.04 538 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", 539 "i486-linux-gnu","", "", triple); 540 // Gentoo amd64 stable 541 AddGnuCPlusPlusIncludePaths( 542 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4", 543 "i686-pc-linux-gnu", "", "", triple); 544 // Exherbo (2009-10-26) 545 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2", 546 "x86_64-pc-linux-gnu", "32", "", triple); 547 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2", 548 "i686-pc-linux-gnu", "", "", triple); 549 break; 550 case llvm::Triple::FreeBSD: 551 // DragonFly 552 AddPath("/usr/include/c++/4.1", System, true, false, false); 553 // FreeBSD 554 AddPath("/usr/include/c++/4.2", System, true, false, false); 555 break; 556 case llvm::Triple::Solaris: 557 // Solaris - Fall though.. 558 case llvm::Triple::AuroraUX: 559 // AuroraUX 560 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4", 561 "i386-pc-solaris2.11", "", "", triple); 562 break; 563 default: 564 break; 565 } 566} 567 568void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang, 569 const llvm::Triple &triple) { 570 if (Lang.CPlusPlus) 571 AddDefaultCPlusPlusIncludePaths(triple); 572 573 AddDefaultCIncludePaths(triple); 574 575 // Add the default framework include paths on Darwin. 576 if (triple.getOS() == llvm::Triple::Darwin) { 577 AddPath("/System/Library/Frameworks", System, true, false, true); 578 AddPath("/Library/Frameworks", System, true, false, true); 579 } 580} 581 582/// RemoveDuplicates - If there are duplicate directory entries in the specified 583/// search list, remove the later (dead) ones. 584static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList, 585 bool Verbose) { 586 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; 587 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; 588 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; 589 for (unsigned i = 0; i != SearchList.size(); ++i) { 590 unsigned DirToRemove = i; 591 592 const DirectoryLookup &CurEntry = SearchList[i]; 593 594 if (CurEntry.isNormalDir()) { 595 // If this isn't the first time we've seen this dir, remove it. 596 if (SeenDirs.insert(CurEntry.getDir())) 597 continue; 598 } else if (CurEntry.isFramework()) { 599 // If this isn't the first time we've seen this framework dir, remove it. 600 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir())) 601 continue; 602 } else { 603 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); 604 // If this isn't the first time we've seen this headermap, remove it. 605 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap())) 606 continue; 607 } 608 609 // If we have a normal #include dir/framework/headermap that is shadowed 610 // later in the chain by a system include location, we actually want to 611 // ignore the user's request and drop the user dir... keeping the system 612 // dir. This is weird, but required to emulate GCC's search path correctly. 613 // 614 // Since dupes of system dirs are rare, just rescan to find the original 615 // that we're nuking instead of using a DenseMap. 616 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) { 617 // Find the dir that this is the same of. 618 unsigned FirstDir; 619 for (FirstDir = 0; ; ++FirstDir) { 620 assert(FirstDir != i && "Didn't find dupe?"); 621 622 const DirectoryLookup &SearchEntry = SearchList[FirstDir]; 623 624 // If these are different lookup types, then they can't be the dupe. 625 if (SearchEntry.getLookupType() != CurEntry.getLookupType()) 626 continue; 627 628 bool isSame; 629 if (CurEntry.isNormalDir()) 630 isSame = SearchEntry.getDir() == CurEntry.getDir(); 631 else if (CurEntry.isFramework()) 632 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir(); 633 else { 634 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); 635 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap(); 636 } 637 638 if (isSame) 639 break; 640 } 641 642 // If the first dir in the search path is a non-system dir, zap it 643 // instead of the system one. 644 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User) 645 DirToRemove = FirstDir; 646 } 647 648 if (Verbose) { 649 llvm::errs() << "ignoring duplicate directory \"" 650 << CurEntry.getName() << "\"\n"; 651 if (DirToRemove != i) 652 llvm::errs() << " as it is a non-system directory that duplicates " 653 << "a system directory\n"; 654 } 655 656 // This is reached if the current entry is a duplicate. Remove the 657 // DirToRemove (usually the current dir). 658 SearchList.erase(SearchList.begin()+DirToRemove); 659 --i; 660 } 661} 662 663 664void InitHeaderSearch::Realize() { 665 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. 666 std::vector<DirectoryLookup> SearchList; 667 SearchList = IncludeGroup[Angled]; 668 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(), 669 IncludeGroup[System].end()); 670 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(), 671 IncludeGroup[After].end()); 672 RemoveDuplicates(SearchList, Verbose); 673 RemoveDuplicates(IncludeGroup[Quoted], Verbose); 674 675 // Prepend QUOTED list on the search list. 676 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(), 677 IncludeGroup[Quoted].end()); 678 679 680 bool DontSearchCurDir = false; // TODO: set to true if -I- is set? 681 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(), 682 DontSearchCurDir); 683 684 // If verbose, print the list of directories that will be searched. 685 if (Verbose) { 686 llvm::errs() << "#include \"...\" search starts here:\n"; 687 unsigned QuotedIdx = IncludeGroup[Quoted].size(); 688 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { 689 if (i == QuotedIdx) 690 llvm::errs() << "#include <...> search starts here:\n"; 691 const char *Name = SearchList[i].getName(); 692 const char *Suffix; 693 if (SearchList[i].isNormalDir()) 694 Suffix = ""; 695 else if (SearchList[i].isFramework()) 696 Suffix = " (framework directory)"; 697 else { 698 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup"); 699 Suffix = " (headermap)"; 700 } 701 llvm::errs() << " " << Name << Suffix << "\n"; 702 } 703 llvm::errs() << "End of search list.\n"; 704 } 705} 706 707void clang::ApplyHeaderSearchOptions(HeaderSearch &HS, 708 const HeaderSearchOptions &HSOpts, 709 const LangOptions &Lang, 710 const llvm::Triple &Triple) { 711 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot); 712 713 // Add the user defined entries. 714 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) { 715 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i]; 716 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework, 717 false); 718 } 719 720 // Add entries from CPATH and friends. 721 Init.AddDelimitedPaths(HSOpts.EnvIncPath.c_str()); 722 if (Lang.CPlusPlus && Lang.ObjC1) 723 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath.c_str()); 724 else if (Lang.CPlusPlus) 725 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath.c_str()); 726 else if (Lang.ObjC1) 727 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath.c_str()); 728 else 729 Init.AddDelimitedPaths(HSOpts.CEnvIncPath.c_str()); 730 731 if (!HSOpts.BuiltinIncludePath.empty()) { 732 // Ignore the sys root, we *always* look for clang headers relative to 733 // supplied path. 734 Init.AddPath(HSOpts.BuiltinIncludePath, System, 735 false, false, false, /*IgnoreSysRoot=*/ true); 736 } 737 738 if (HSOpts.UseStandardIncludes) 739 Init.AddDefaultSystemIncludePaths(Lang, Triple); 740 741 Init.Realize(); 742} 743