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