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