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