InitHeaderSearch.cpp revision ef888a4d7f0fa1596807f702d09df88044dcaf41
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/System/Path.h" 21#include "llvm/Config/config.h" 22#include <cstdio> 23#include <vector> 24using namespace clang; 25 26void InitHeaderSearch::AddPath(const std::string &Path, IncludeDirGroup Group, 27 bool isCXXAware, bool isUserSupplied, 28 bool isFramework, bool IgnoreSysRoot) { 29 assert(!Path.empty() && "can't handle empty path here"); 30 FileManager &FM = Headers.getFileMgr(); 31 32 // Compute the actual path, taking into consideration -isysroot. 33 llvm::SmallString<256> MappedPath; 34 35 // Handle isysroot. 36 if (Group == System && !IgnoreSysRoot) { 37 // FIXME: Portability. This should be a sys::Path interface, this doesn't 38 // handle things like C:\ right, nor win32 \\network\device\blah. 39 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present. 40 MappedPath.append(isysroot.begin(), isysroot.end()); 41 } 42 43 MappedPath.append(Path.begin(), Path.end()); 44 45 // Compute the DirectoryLookup type. 46 SrcMgr::CharacteristicKind Type; 47 if (Group == Quoted || Group == Angled) 48 Type = SrcMgr::C_User; 49 else if (isCXXAware) 50 Type = SrcMgr::C_System; 51 else 52 Type = SrcMgr::C_ExternCSystem; 53 54 55 // If the directory exists, add it. 56 if (const DirectoryEntry *DE = FM.getDirectory(&MappedPath[0], 57 &MappedPath[0]+ 58 MappedPath.size())) { 59 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied, 60 isFramework)); 61 return; 62 } 63 64 // Check to see if this is an apple-style headermap (which are not allowed to 65 // be frameworks). 66 if (!isFramework) { 67 if (const FileEntry *FE = FM.getFile(&MappedPath[0], 68 &MappedPath[0]+MappedPath.size())) { 69 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) { 70 // It is a headermap, add it to the search path. 71 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied)); 72 return; 73 } 74 } 75 } 76 77 if (Verbose) 78 fprintf(stderr, "ignoring nonexistent directory \"%s\"\n", 79 MappedPath.c_str()); 80} 81 82 83void InitHeaderSearch::AddEnvVarPaths(const char *Name) { 84 const char* at = getenv(Name); 85 if (!at || *at == 0) // Empty string should not add '.' path. 86 return; 87 88 const char* delim = strchr(at, llvm::sys::PathSeparator); 89 while (delim != 0) { 90 if (delim-at == 0) 91 AddPath(".", Angled, false, true, false); 92 else 93 AddPath(std::string(at, std::string::size_type(delim-at)), Angled, false, 94 true, false); 95 at = delim + 1; 96 delim = strchr(at, llvm::sys::PathSeparator); 97 } 98 if (*at == 0) 99 AddPath(".", Angled, false, true, false); 100 else 101 AddPath(at, Angled, false, true, false); 102} 103 104 105void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang) { 106 // FIXME: temporary hack: hard-coded paths. 107 // FIXME: get these from the target? 108 109#ifdef LLVM_ON_WIN32 110 if (Lang.CPlusPlus) { 111 // Mingw32 GCC version 4 112 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++", 113 System, true, false, false); 114 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/mingw32", 115 System, true, false, false); 116 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/backward", 117 System, true, false, false); 118 } 119 120 // Mingw32 GCC version 4 121 AddPath("C:/mingw/include", System, false, false, false); 122#else 123 124 if (Lang.CPlusPlus) { 125 AddPath("/usr/include/c++/4.2.1", System, true, false, false); 126 AddPath("/usr/include/c++/4.2.1/i686-apple-darwin10", System, true, false, 127 false); 128 AddPath("/usr/include/c++/4.2.1/backward", System, true, false, false); 129 130 AddPath("/usr/include/c++/4.0.0", System, true, false, false); 131 AddPath("/usr/include/c++/4.0.0/i686-apple-darwin8", System, true, false, 132 false); 133 AddPath("/usr/include/c++/4.0.0/backward", System, true, false, false); 134 135 // Ubuntu 7.10 - Gutsy Gibbon 136 AddPath("/usr/include/c++/4.1.3", System, true, false, false); 137 AddPath("/usr/include/c++/4.1.3/i486-linux-gnu", System, true, false, 138 false); 139 AddPath("/usr/include/c++/4.1.3/backward", System, true, false, false); 140 141 // Ubuntu 9.04 142 AddPath("/usr/include/c++/4.3.3", System, true, false, false); 143 AddPath("/usr/include/c++/4.3.3/x86_64-linux-gnu/", System, true, false, 144 false); 145 AddPath("/usr/include/c++/4.3.3/backward", System, true, false, false); 146 147 // Fedora 8 148 AddPath("/usr/include/c++/4.1.2", System, true, false, false); 149 AddPath("/usr/include/c++/4.1.2/i386-redhat-linux", System, true, false, 150 false); 151 AddPath("/usr/include/c++/4.1.2/backward", System, true, false, false); 152 153 // Fedora 9 154 AddPath("/usr/include/c++/4.3.0", System, true, false, false); 155 AddPath("/usr/include/c++/4.3.0/i386-redhat-linux", System, true, false, 156 false); 157 AddPath("/usr/include/c++/4.3.0/backward", System, true, false, false); 158 159 // Fedora 10 160 AddPath("/usr/include/c++/4.3.2", System, true, false, false); 161 AddPath("/usr/include/c++/4.3.2/i386-redhat-linux", System, true, false, 162 false); 163 AddPath("/usr/include/c++/4.3.2/backward", System, true, false, false); 164 165 // openSUSE 11.1 166 AddPath("/usr/include/c++/4.3", System, true, false, false); 167 AddPath("/usr/include/c++/4.3/i586-suse-linux", System, true, false, 168 false); 169 AddPath("/usr/include/c++/4.3/x86_64-suse-linux", System, true, false, 170 false); 171 AddPath("/usr/include/c++/4.3/backward", System, true, false, false); 172 173 // openSUSE 11.2 174 AddPath("/usr/include/c++/4.4", System, true, false, false); 175 AddPath("/usr/include/c++/4.4/i586-suse-linux", System, true, false, 176 false); 177 AddPath("/usr/include/c++/4.4/x86_64-suse-linux", System, true, false, 178 false); 179 AddPath("/usr/include/c++/4.4/backward", System, true, false, false); 180 181 // Arch Linux 2008-06-24 182 AddPath("/usr/include/c++/4.3.1", System, true, false, false); 183 AddPath("/usr/include/c++/4.3.1/i686-pc-linux-gnu", System, true, false, 184 false); 185 AddPath("/usr/include/c++/4.3.1/backward", System, true, false, false); 186 AddPath("/usr/include/c++/4.3.1/x86_64-unknown-linux-gnu", System, true, 187 false, false); 188 189 // Gentoo x86 2009.0 stable 190 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4", System, 191 true, false, false); 192 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/" 193 "i686-pc-linux-gnu", System, true, false, false); 194 AddPath(" /usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/backward", 195 System, true, false, false); 196 197 // Gentoo x86 2008.0 stable 198 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4", System, 199 true, false, false); 200 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/" 201 "i686-pc-linux-gnu", System, true, false, false); 202 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/backward", 203 System, true, false, false); 204 205 // Gentoo amd64 stable 206 AddPath("/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4", System, 207 true, false, false); 208 AddPath("/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/" 209 "i686-pc-linux-gnu", System, true, false, false); 210 AddPath("/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/backward", 211 System, true, false, false); 212 213 // DragonFly 214 AddPath("/usr/include/c++/4.1", System, true, false, false); 215 216 // FreeBSD 217 AddPath("/usr/include/c++/4.2", System, true, false, false); 218 219 // AuroraUX 220 AddPath("/opt/gcc4/include/c++/4.2.4", System, true, false, false); 221 AddPath("/opt/gcc4/include/c++/4.2.4/i386-pc-solaris2.11", System, true, false, false); 222 } 223 224 AddPath("/usr/local/include", System, false, false, false); 225 226 AddPath("/usr/include", System, false, false, false); 227 AddPath("/System/Library/Frameworks", System, true, false, true); 228 AddPath("/Library/Frameworks", System, true, false, true); 229#endif 230} 231 232void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) { 233 AddEnvVarPaths("CPATH"); 234 if (Lang.CPlusPlus && Lang.ObjC1) 235 AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH"); 236 else if (Lang.CPlusPlus) 237 AddEnvVarPaths("CPLUS_INCLUDE_PATH"); 238 else if (Lang.ObjC1) 239 AddEnvVarPaths("OBJC_INCLUDE_PATH"); 240 else 241 AddEnvVarPaths("C_INCLUDE_PATH"); 242} 243 244 245/// RemoveDuplicates - If there are duplicate directory entries in the specified 246/// search list, remove the later (dead) ones. 247static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList, 248 bool Verbose) { 249 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; 250 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; 251 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; 252 for (unsigned i = 0; i != SearchList.size(); ++i) { 253 unsigned DirToRemove = i; 254 255 const DirectoryLookup &CurEntry = SearchList[i]; 256 257 if (CurEntry.isNormalDir()) { 258 // If this isn't the first time we've seen this dir, remove it. 259 if (SeenDirs.insert(CurEntry.getDir())) 260 continue; 261 } else if (CurEntry.isFramework()) { 262 // If this isn't the first time we've seen this framework dir, remove it. 263 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir())) 264 continue; 265 } else { 266 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); 267 // If this isn't the first time we've seen this headermap, remove it. 268 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap())) 269 continue; 270 } 271 272 // If we have a normal #include dir/framework/headermap that is shadowed 273 // later in the chain by a system include location, we actually want to 274 // ignore the user's request and drop the user dir... keeping the system 275 // dir. This is weird, but required to emulate GCC's search path correctly. 276 // 277 // Since dupes of system dirs are rare, just rescan to find the original 278 // that we're nuking instead of using a DenseMap. 279 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) { 280 // Find the dir that this is the same of. 281 unsigned FirstDir; 282 for (FirstDir = 0; ; ++FirstDir) { 283 assert(FirstDir != i && "Didn't find dupe?"); 284 285 const DirectoryLookup &SearchEntry = SearchList[FirstDir]; 286 287 // If these are different lookup types, then they can't be the dupe. 288 if (SearchEntry.getLookupType() != CurEntry.getLookupType()) 289 continue; 290 291 bool isSame; 292 if (CurEntry.isNormalDir()) 293 isSame = SearchEntry.getDir() == CurEntry.getDir(); 294 else if (CurEntry.isFramework()) 295 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir(); 296 else { 297 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); 298 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap(); 299 } 300 301 if (isSame) 302 break; 303 } 304 305 // If the first dir in the search path is a non-system dir, zap it 306 // instead of the system one. 307 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User) 308 DirToRemove = FirstDir; 309 } 310 311 if (Verbose) { 312 fprintf(stderr, "ignoring duplicate directory \"%s\"\n", 313 CurEntry.getName()); 314 if (DirToRemove != i) 315 fprintf(stderr, " as it is a non-system directory that duplicates" 316 " a system directory\n"); 317 } 318 319 // This is reached if the current entry is a duplicate. Remove the 320 // DirToRemove (usually the current dir). 321 SearchList.erase(SearchList.begin()+DirToRemove); 322 --i; 323 } 324} 325 326 327void InitHeaderSearch::Realize() { 328 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. 329 std::vector<DirectoryLookup> SearchList; 330 SearchList = IncludeGroup[Angled]; 331 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(), 332 IncludeGroup[System].end()); 333 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(), 334 IncludeGroup[After].end()); 335 RemoveDuplicates(SearchList, Verbose); 336 RemoveDuplicates(IncludeGroup[Quoted], Verbose); 337 338 // Prepend QUOTED list on the search list. 339 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(), 340 IncludeGroup[Quoted].end()); 341 342 343 bool DontSearchCurDir = false; // TODO: set to true if -I- is set? 344 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(), 345 DontSearchCurDir); 346 347 // If verbose, print the list of directories that will be searched. 348 if (Verbose) { 349 fprintf(stderr, "#include \"...\" search starts here:\n"); 350 unsigned QuotedIdx = IncludeGroup[Quoted].size(); 351 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { 352 if (i == QuotedIdx) 353 fprintf(stderr, "#include <...> search starts here:\n"); 354 const char *Name = SearchList[i].getName(); 355 const char *Suffix; 356 if (SearchList[i].isNormalDir()) 357 Suffix = ""; 358 else if (SearchList[i].isFramework()) 359 Suffix = " (framework directory)"; 360 else { 361 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup"); 362 Suffix = " (headermap)"; 363 } 364 fprintf(stderr, " %s%s\n", Name, Suffix); 365 } 366 fprintf(stderr, "End of search list.\n"); 367 } 368} 369 370