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); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | AddPath("/usr/local/include", System, false, false, false); |
| 150 | |
| 151 | AddPath("/usr/lib/gcc/i686-apple-darwin10/4.2.1/include", System, |
| 152 | false, false, false); |
| 153 | AddPath("/usr/lib/gcc/powerpc-apple-darwin10/4.2.1/include", |
| 154 | System, false, false, false); |
| 155 | |
| 156 | // leopard |
| 157 | AddPath("/usr/lib/gcc/i686-apple-darwin9/4.0.1/include", System, |
| 158 | false, false, false); |
| 159 | AddPath("/usr/lib/gcc/powerpc-apple-darwin9/4.0.1/include", |
| 160 | System, false, false, false); |
| 161 | AddPath("/usr/lib/gcc/powerpc-apple-darwin9/" |
| 162 | "4.0.1/../../../../powerpc-apple-darwin0/include", |
| 163 | System, false, false, false); |
| 164 | |
| 165 | // tiger |
| 166 | AddPath("/usr/lib/gcc/i686-apple-darwin8/4.0.1/include", System, |
| 167 | false, false, false); |
| 168 | AddPath("/usr/lib/gcc/powerpc-apple-darwin8/4.0.1/include", |
| 169 | System, false, false, false); |
| 170 | AddPath("/usr/lib/gcc/powerpc-apple-darwin8/" |
| 171 | "4.0.1/../../../../powerpc-apple-darwin8/include", |
| 172 | System, false, false, false); |
| 173 | |
| 174 | // Ubuntu 7.10 - Gutsy Gibbon |
| 175 | AddPath("/usr/lib/gcc/i486-linux-gnu/4.1.3/include", System, |
| 176 | false, false, false); |
| 177 | |
| 178 | // Fedora 8 |
| 179 | AddPath("/usr/lib/gcc/i386-redhat-linux/4.1.2/include", System, |
| 180 | false, false, false); |
| 181 | |
| 182 | // Fedora 9 |
| 183 | AddPath("/usr/lib/gcc/i386-redhat-linux/4.3.0/include", System, |
| 184 | false, false, false); |
| 185 | |
| 186 | //Debian testing/lenny x86 |
| 187 | AddPath("/usr/lib/gcc/i486-linux-gnu/4.2.3/include", System, |
| 188 | false, false, false); |
| 189 | |
| 190 | //Debian testing/lenny amd64 |
| 191 | AddPath("/usr/lib/gcc/x86_64-linux-gnu/4.2.3/include", System, |
| 192 | false, false, false); |
| 193 | |
| 194 | // Arch Linux 2008-06-24 |
| 195 | AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.1/include", System, |
| 196 | false, false, false); |
| 197 | AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.1/include-fixed", System, |
| 198 | false, false, false); |
| 199 | AddPath("/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/include", System, |
| 200 | false, false, false); |
| 201 | AddPath("/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/include-fixed", |
| 202 | System, false, false, false); |
| 203 | |
| 204 | // Debian testing/lenny ppc32 |
| 205 | AddPath("/usr/lib/gcc/powerpc-linux-gnu/4.2.3/include", System, |
| 206 | false, false, false); |
| 207 | |
| 208 | // Gentoo x86 stable |
| 209 | AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include", System, |
| 210 | false, false, false); |
| 211 | |
Chris Lattner | 5654ffd | 2008-08-23 18:25:07 +0000 | [diff] [blame^] | 212 | // DragonFly |
| 213 | AddPath("/usr/libdata/gcc41", System, true, false, false); |
| 214 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 215 | AddPath("/usr/include", System, false, false, false); |
| 216 | AddPath("/System/Library/Frameworks", System, true, false, true); |
| 217 | AddPath("/Library/Frameworks", System, true, false, true); |
| 218 | } |
| 219 | |
| 220 | void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) { |
| 221 | AddEnvVarPaths("CPATH"); |
| 222 | if (Lang.CPlusPlus && Lang.ObjC1) |
| 223 | AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH"); |
| 224 | else if (Lang.CPlusPlus) |
| 225 | AddEnvVarPaths("CPLUS_INCLUDE_PATH"); |
| 226 | else if (Lang.ObjC1) |
| 227 | AddEnvVarPaths("OBJC_INCLUDE_PATH"); |
| 228 | else |
| 229 | AddEnvVarPaths("C_INCLUDE_PATH"); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /// RemoveDuplicates - If there are duplicate directory entries in the specified |
| 234 | /// search list, remove the later (dead) ones. |
| 235 | static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList, |
| 236 | bool Verbose) { |
| 237 | llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; |
| 238 | llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; |
| 239 | llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; |
| 240 | for (unsigned i = 0; i != SearchList.size(); ++i) { |
| 241 | if (SearchList[i].isNormalDir()) { |
| 242 | // If this isn't the first time we've seen this dir, remove it. |
| 243 | if (SeenDirs.insert(SearchList[i].getDir())) |
| 244 | continue; |
| 245 | |
| 246 | if (Verbose) |
| 247 | fprintf(stderr, "ignoring duplicate directory \"%s\"\n", |
| 248 | SearchList[i].getDir()->getName()); |
| 249 | } else if (SearchList[i].isFramework()) { |
| 250 | // If this isn't the first time we've seen this framework dir, remove it. |
| 251 | if (SeenFrameworkDirs.insert(SearchList[i].getFrameworkDir())) |
| 252 | continue; |
| 253 | |
| 254 | if (Verbose) |
| 255 | fprintf(stderr, "ignoring duplicate framework \"%s\"\n", |
| 256 | SearchList[i].getFrameworkDir()->getName()); |
| 257 | |
| 258 | } else { |
| 259 | assert(SearchList[i].isHeaderMap() && "Not a headermap or normal dir?"); |
| 260 | // If this isn't the first time we've seen this headermap, remove it. |
| 261 | if (SeenHeaderMaps.insert(SearchList[i].getHeaderMap())) |
| 262 | continue; |
| 263 | |
| 264 | if (Verbose) |
| 265 | fprintf(stderr, "ignoring duplicate directory \"%s\"\n", |
| 266 | SearchList[i].getDir()->getName()); |
| 267 | } |
| 268 | |
| 269 | // This is reached if the current entry is a duplicate. |
| 270 | SearchList.erase(SearchList.begin()+i); |
| 271 | --i; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | |
| 276 | void InitHeaderSearch::Realize() { |
| 277 | // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. |
| 278 | std::vector<DirectoryLookup> SearchList; |
| 279 | SearchList = IncludeGroup[Angled]; |
| 280 | SearchList.insert(SearchList.end(), IncludeGroup[System].begin(), |
| 281 | IncludeGroup[System].end()); |
| 282 | SearchList.insert(SearchList.end(), IncludeGroup[After].begin(), |
| 283 | IncludeGroup[After].end()); |
| 284 | RemoveDuplicates(SearchList, Verbose); |
| 285 | RemoveDuplicates(IncludeGroup[Quoted], Verbose); |
| 286 | |
| 287 | // Prepend QUOTED list on the search list. |
| 288 | SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(), |
| 289 | IncludeGroup[Quoted].end()); |
| 290 | |
| 291 | |
| 292 | bool DontSearchCurDir = false; // TODO: set to true if -I- is set? |
| 293 | Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(), |
| 294 | DontSearchCurDir); |
| 295 | |
| 296 | // If verbose, print the list of directories that will be searched. |
| 297 | if (Verbose) { |
| 298 | fprintf(stderr, "#include \"...\" search starts here:\n"); |
| 299 | unsigned QuotedIdx = IncludeGroup[Quoted].size(); |
| 300 | for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { |
| 301 | if (i == QuotedIdx) |
| 302 | fprintf(stderr, "#include <...> search starts here:\n"); |
| 303 | const char *Name = SearchList[i].getName(); |
| 304 | const char *Suffix; |
| 305 | if (SearchList[i].isNormalDir()) |
| 306 | Suffix = ""; |
| 307 | else if (SearchList[i].isFramework()) |
| 308 | Suffix = " (framework directory)"; |
| 309 | else { |
| 310 | assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup"); |
| 311 | Suffix = " (headermap)"; |
| 312 | } |
| 313 | fprintf(stderr, " %s%s\n", Name, Suffix); |
| 314 | } |
| 315 | fprintf(stderr, "End of search list.\n"); |
| 316 | } |
| 317 | } |
| 318 | |