Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 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/Driver/InitHeaderSearch.h" |
| 15 | |
| 16 | #include "clang/Lex/HeaderSearch.h" |
| 17 | #include "clang/Basic/FileManager.h" |
| 18 | #include "clang/Basic/LangOptions.h" |
| 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | #include "llvm/System/Path.h" |
| 22 | |
| 23 | #include <vector> |
| 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | void InitHeaderSearch::AddPath(const std::string &Path, IncludeDirGroup Group, |
| 28 | bool isCXXAware, bool isUserSupplied, |
| 29 | bool isFramework) { |
| 30 | assert(!Path.empty() && "can't handle empty path here"); |
| 31 | FileManager &FM = Headers.getFileMgr(); |
| 32 | |
| 33 | // Compute the actual path, taking into consideration -isysroot. |
| 34 | llvm::SmallString<256> MappedPath; |
| 35 | |
| 36 | // Handle isysroot. |
| 37 | if (Group == System) { |
| 38 | // FIXME: Portability. This should be a sys::Path interface, this doesn't |
| 39 | // handle things like C:\ right, nor win32 \\network\device\blah. |
| 40 | if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present. |
| 41 | MappedPath.append(isysroot.begin(), isysroot.end()); |
| 42 | } |
| 43 | |
| 44 | MappedPath.append(Path.begin(), Path.end()); |
| 45 | |
| 46 | // Compute the DirectoryLookup type. |
| 47 | DirectoryLookup::DirType Type; |
| 48 | if (Group == Quoted || Group == Angled) |
| 49 | Type = DirectoryLookup::NormalHeaderDir; |
| 50 | else if (isCXXAware) |
| 51 | Type = DirectoryLookup::SystemHeaderDir; |
| 52 | else |
| 53 | Type = DirectoryLookup::ExternCSystemHeaderDir; |
| 54 | |
| 55 | |
| 56 | // If the directory exists, add it. |
| 57 | if (const DirectoryEntry *DE = FM.getDirectory(&MappedPath[0], |
| 58 | &MappedPath[0]+ |
| 59 | MappedPath.size())) { |
| 60 | IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied, |
| 61 | isFramework)); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | // Check to see if this is an apple-style headermap (which are not allowed to |
| 66 | // be frameworks). |
| 67 | if (!isFramework) { |
| 68 | if (const FileEntry *FE = FM.getFile(&MappedPath[0], |
| 69 | &MappedPath[0]+MappedPath.size())) { |
| 70 | if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) { |
| 71 | // It is a headermap, add it to the search path. |
| 72 | IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied)); |
| 73 | return; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if (Verbose) |
| 79 | fprintf(stderr, "ignoring nonexistent directory \"%s\"\n", Path.c_str()); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | void InitHeaderSearch::AddEnvVarPaths(const char *Name) { |
| 84 | const char* at = getenv(Name); |
| 85 | if (!at) |
| 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 | |
| 105 | void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang) { |
| 106 | // FIXME: temporary hack: hard-coded paths. |
| 107 | // FIXME: get these from the target? |
| 108 | if (Lang.CPlusPlus) { |
| 109 | AddPath("/usr/include/c++/4.2.1", System, true, false, false); |
| 110 | AddPath("/usr/include/c++/4.2.1/i686-apple-darwin10", System, true, false, |
| 111 | false); |
| 112 | AddPath("/usr/include/c++/4.2.1/backward", System, true, false, false); |
| 113 | |
| 114 | AddPath("/usr/include/c++/4.0.0", System, true, false, false); |
| 115 | AddPath("/usr/include/c++/4.0.0/i686-apple-darwin8", System, true, false, |
| 116 | false); |
| 117 | AddPath("/usr/include/c++/4.0.0/backward", System, true, false, false); |
| 118 | |
| 119 | // Ubuntu 7.10 - Gutsy Gibbon |
| 120 | AddPath("/usr/include/c++/4.1.3", System, true, false, false); |
| 121 | AddPath("/usr/include/c++/4.1.3/i486-linux-gnu", System, true, false, |
| 122 | false); |
| 123 | AddPath("/usr/include/c++/4.1.3/backward", System, true, false, false); |
| 124 | |
| 125 | // Fedora 8 |
| 126 | AddPath("/usr/include/c++/4.1.2", System, true, false, false); |
| 127 | AddPath("/usr/include/c++/4.1.2/i386-redhat-linux", System, true, false, |
| 128 | false); |
| 129 | AddPath("/usr/include/c++/4.1.2/backward", System, true, false, false); |
| 130 | |
| 131 | // Fedora 9 |
| 132 | AddPath("/usr/include/c++/4.3.0", System, true, false, false); |
| 133 | AddPath("/usr/include/c++/4.3.0/i386-redhat-linux", System, true, false, |
| 134 | false); |
| 135 | AddPath("/usr/include/c++/4.3.0/backward", System, true, false, false); |
| 136 | |
| 137 | // Arch Linux 2008-06-24 |
| 138 | AddPath("/usr/include/c++/4.3.1", System, true, false, false); |
| 139 | AddPath("/usr/include/c++/4.3.1/i686-pc-linux-gnu", System, true, false, |
| 140 | false); |
| 141 | AddPath("/usr/include/c++/4.3.1/backward", System, true, false, false); |
| 142 | AddPath("/usr/include/c++/4.3.1/x86_64-unknown-linux-gnu", System, true, |
| 143 | false, false); |
Chris Lattner | 5654ffd | 2008-08-23 18:25:07 +0000 | [diff] [blame] | 144 | |
| 145 | // DragonFly |
| 146 | AddPath("/usr/include/c++/4.1", System, true, false, false); |
Argyrios Kyrtzidis | bdbd462 | 2008-09-05 09:03:53 +0000 | [diff] [blame^] | 147 | |
| 148 | // Mingw32 GCC version 4 |
| 149 | AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++", System, true, false, false); |
| 150 | AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/mingw32", System, true, false, false); |
| 151 | AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/backward", System, true, false, false); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | AddPath("/usr/local/include", System, false, false, false); |
| 155 | |
| 156 | AddPath("/usr/lib/gcc/i686-apple-darwin10/4.2.1/include", System, |
| 157 | false, false, false); |
| 158 | AddPath("/usr/lib/gcc/powerpc-apple-darwin10/4.2.1/include", |
| 159 | System, false, false, false); |
| 160 | |
| 161 | // leopard |
| 162 | AddPath("/usr/lib/gcc/i686-apple-darwin9/4.0.1/include", System, |
| 163 | false, false, false); |
| 164 | AddPath("/usr/lib/gcc/powerpc-apple-darwin9/4.0.1/include", |
| 165 | System, false, false, false); |
| 166 | AddPath("/usr/lib/gcc/powerpc-apple-darwin9/" |
| 167 | "4.0.1/../../../../powerpc-apple-darwin0/include", |
| 168 | System, false, false, false); |
| 169 | |
| 170 | // tiger |
| 171 | AddPath("/usr/lib/gcc/i686-apple-darwin8/4.0.1/include", System, |
| 172 | false, false, false); |
| 173 | AddPath("/usr/lib/gcc/powerpc-apple-darwin8/4.0.1/include", |
| 174 | System, false, false, false); |
| 175 | AddPath("/usr/lib/gcc/powerpc-apple-darwin8/" |
| 176 | "4.0.1/../../../../powerpc-apple-darwin8/include", |
| 177 | System, false, false, false); |
| 178 | |
| 179 | // Ubuntu 7.10 - Gutsy Gibbon |
| 180 | AddPath("/usr/lib/gcc/i486-linux-gnu/4.1.3/include", System, |
| 181 | false, false, false); |
| 182 | |
| 183 | // Fedora 8 |
| 184 | AddPath("/usr/lib/gcc/i386-redhat-linux/4.1.2/include", System, |
| 185 | false, false, false); |
| 186 | |
| 187 | // Fedora 9 |
| 188 | AddPath("/usr/lib/gcc/i386-redhat-linux/4.3.0/include", System, |
| 189 | false, false, false); |
| 190 | |
| 191 | //Debian testing/lenny x86 |
| 192 | AddPath("/usr/lib/gcc/i486-linux-gnu/4.2.3/include", System, |
| 193 | false, false, false); |
| 194 | |
| 195 | //Debian testing/lenny amd64 |
| 196 | AddPath("/usr/lib/gcc/x86_64-linux-gnu/4.2.3/include", System, |
| 197 | false, false, false); |
| 198 | |
| 199 | // Arch Linux 2008-06-24 |
| 200 | AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.1/include", System, |
| 201 | false, false, false); |
| 202 | AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.1/include-fixed", System, |
| 203 | false, false, false); |
| 204 | AddPath("/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/include", System, |
| 205 | false, false, false); |
| 206 | AddPath("/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/include-fixed", |
| 207 | System, false, false, false); |
| 208 | |
| 209 | // Debian testing/lenny ppc32 |
| 210 | AddPath("/usr/lib/gcc/powerpc-linux-gnu/4.2.3/include", System, |
| 211 | false, false, false); |
| 212 | |
| 213 | // Gentoo x86 stable |
| 214 | AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include", System, |
| 215 | false, false, false); |
| 216 | |
Chris Lattner | 5654ffd | 2008-08-23 18:25:07 +0000 | [diff] [blame] | 217 | // DragonFly |
| 218 | AddPath("/usr/libdata/gcc41", System, true, false, false); |
| 219 | |
Argyrios Kyrtzidis | bdbd462 | 2008-09-05 09:03:53 +0000 | [diff] [blame^] | 220 | // Mingw32 GCC version 4 |
| 221 | AddPath("C:/mingw/lib/gcc/mingw32/4.3.0/include", System, false, false, false); |
| 222 | AddPath("C:/mingw/include", System, false, false, false); |
| 223 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 224 | AddPath("/usr/include", System, false, false, false); |
| 225 | AddPath("/System/Library/Frameworks", System, true, false, true); |
| 226 | AddPath("/Library/Frameworks", System, true, false, true); |
| 227 | } |
| 228 | |
| 229 | void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) { |
| 230 | AddEnvVarPaths("CPATH"); |
| 231 | if (Lang.CPlusPlus && Lang.ObjC1) |
| 232 | AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH"); |
| 233 | else if (Lang.CPlusPlus) |
| 234 | AddEnvVarPaths("CPLUS_INCLUDE_PATH"); |
| 235 | else if (Lang.ObjC1) |
| 236 | AddEnvVarPaths("OBJC_INCLUDE_PATH"); |
| 237 | else |
| 238 | AddEnvVarPaths("C_INCLUDE_PATH"); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /// RemoveDuplicates - If there are duplicate directory entries in the specified |
| 243 | /// search list, remove the later (dead) ones. |
| 244 | static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList, |
| 245 | bool Verbose) { |
| 246 | llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; |
| 247 | llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; |
| 248 | llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; |
| 249 | for (unsigned i = 0; i != SearchList.size(); ++i) { |
| 250 | if (SearchList[i].isNormalDir()) { |
| 251 | // If this isn't the first time we've seen this dir, remove it. |
| 252 | if (SeenDirs.insert(SearchList[i].getDir())) |
| 253 | continue; |
| 254 | |
| 255 | if (Verbose) |
| 256 | fprintf(stderr, "ignoring duplicate directory \"%s\"\n", |
| 257 | SearchList[i].getDir()->getName()); |
| 258 | } else if (SearchList[i].isFramework()) { |
| 259 | // If this isn't the first time we've seen this framework dir, remove it. |
| 260 | if (SeenFrameworkDirs.insert(SearchList[i].getFrameworkDir())) |
| 261 | continue; |
| 262 | |
| 263 | if (Verbose) |
| 264 | fprintf(stderr, "ignoring duplicate framework \"%s\"\n", |
| 265 | SearchList[i].getFrameworkDir()->getName()); |
| 266 | |
| 267 | } else { |
| 268 | assert(SearchList[i].isHeaderMap() && "Not a headermap or normal dir?"); |
| 269 | // If this isn't the first time we've seen this headermap, remove it. |
| 270 | if (SeenHeaderMaps.insert(SearchList[i].getHeaderMap())) |
| 271 | continue; |
| 272 | |
| 273 | if (Verbose) |
| 274 | fprintf(stderr, "ignoring duplicate directory \"%s\"\n", |
| 275 | SearchList[i].getDir()->getName()); |
| 276 | } |
| 277 | |
| 278 | // This is reached if the current entry is a duplicate. |
| 279 | SearchList.erase(SearchList.begin()+i); |
| 280 | --i; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | |
| 285 | void InitHeaderSearch::Realize() { |
| 286 | // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. |
| 287 | std::vector<DirectoryLookup> SearchList; |
| 288 | SearchList = IncludeGroup[Angled]; |
| 289 | SearchList.insert(SearchList.end(), IncludeGroup[System].begin(), |
| 290 | IncludeGroup[System].end()); |
| 291 | SearchList.insert(SearchList.end(), IncludeGroup[After].begin(), |
| 292 | IncludeGroup[After].end()); |
| 293 | RemoveDuplicates(SearchList, Verbose); |
| 294 | RemoveDuplicates(IncludeGroup[Quoted], Verbose); |
| 295 | |
| 296 | // Prepend QUOTED list on the search list. |
| 297 | SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(), |
| 298 | IncludeGroup[Quoted].end()); |
| 299 | |
| 300 | |
| 301 | bool DontSearchCurDir = false; // TODO: set to true if -I- is set? |
| 302 | Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(), |
| 303 | DontSearchCurDir); |
| 304 | |
| 305 | // If verbose, print the list of directories that will be searched. |
| 306 | if (Verbose) { |
| 307 | fprintf(stderr, "#include \"...\" search starts here:\n"); |
| 308 | unsigned QuotedIdx = IncludeGroup[Quoted].size(); |
| 309 | for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { |
| 310 | if (i == QuotedIdx) |
| 311 | fprintf(stderr, "#include <...> search starts here:\n"); |
| 312 | const char *Name = SearchList[i].getName(); |
| 313 | const char *Suffix; |
| 314 | if (SearchList[i].isNormalDir()) |
| 315 | Suffix = ""; |
| 316 | else if (SearchList[i].isFramework()) |
| 317 | Suffix = " (framework directory)"; |
| 318 | else { |
| 319 | assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup"); |
| 320 | Suffix = " (headermap)"; |
| 321 | } |
| 322 | fprintf(stderr, " %s%s\n", Name, Suffix); |
| 323 | } |
| 324 | fprintf(stderr, "End of search list.\n"); |
| 325 | } |
| 326 | } |
| 327 | |