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