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 | |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/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" |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 21 | #include "llvm/System/Path.h" |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 22 | #include "llvm/Config/config.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 23 | #include <cstdio> |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 26 | void InitHeaderSearch::AddPath(const llvm::StringRef &Path, |
| 27 | IncludeDirGroup Group, bool isCXXAware, |
| 28 | bool isUserSupplied, bool isFramework, |
| 29 | bool IgnoreSysRoot) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 30 | assert(!Path.empty() && "can't handle empty path here"); |
| 31 | FileManager &FM = Headers.getFileMgr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 33 | // Compute the actual path, taking into consideration -isysroot. |
| 34 | llvm::SmallString<256> MappedPath; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 36 | // Handle isysroot. |
Chris Lattner | 6858dd3 | 2009-02-19 06:48:28 +0000 | [diff] [blame] | 37 | if (Group == System && !IgnoreSysRoot) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 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 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 44 | MappedPath.append(Path.begin(), Path.end()); |
| 45 | |
| 46 | // Compute the DirectoryLookup type. |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 47 | SrcMgr::CharacteristicKind Type; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 48 | if (Group == Quoted || Group == Angled) |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 49 | Type = SrcMgr::C_User; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 50 | else if (isCXXAware) |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 51 | Type = SrcMgr::C_System; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 52 | else |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 53 | Type = SrcMgr::C_ExternCSystem; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
| 55 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 56 | // If the directory exists, add it. |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 57 | if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 58 | IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied, |
| 59 | isFramework)); |
| 60 | return; |
| 61 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 63 | // Check to see if this is an apple-style headermap (which are not allowed to |
| 64 | // be frameworks). |
| 65 | if (!isFramework) { |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 66 | if (const FileEntry *FE = FM.getFile(MappedPath.str())) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 67 | if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) { |
| 68 | // It is a headermap, add it to the search path. |
| 69 | IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied)); |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 75 | if (Verbose) |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 76 | llvm::errs() << "ignoring nonexistent directory \"" |
| 77 | << MappedPath.str() << "\"\n"; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
| 81 | void InitHeaderSearch::AddEnvVarPaths(const char *Name) { |
| 82 | const char* at = getenv(Name); |
Daniel Dunbar | bb95255 | 2008-10-04 20:58:18 +0000 | [diff] [blame] | 83 | if (!at || *at == 0) // Empty string should not add '.' path. |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 84 | return; |
| 85 | |
| 86 | const char* delim = strchr(at, llvm::sys::PathSeparator); |
| 87 | while (delim != 0) { |
| 88 | if (delim-at == 0) |
| 89 | AddPath(".", Angled, false, true, false); |
| 90 | else |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 91 | AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 92 | at = delim + 1; |
| 93 | delim = strchr(at, llvm::sys::PathSeparator); |
| 94 | } |
| 95 | if (*at == 0) |
| 96 | AddPath(".", Angled, false, true, false); |
| 97 | else |
| 98 | AddPath(at, Angled, false, true, false); |
| 99 | } |
| 100 | |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 101 | void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(std::string base, |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 102 | std::string arch) { |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 103 | AddPath(base, System, true, false, false); |
| 104 | AddPath(base + "/" + arch, System, true, false, false); |
| 105 | AddPath(base + "/backward", System, true, false, false); |
| 106 | } |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 107 | |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 108 | #if defined(LLVM_ON_WIN32) |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 109 | |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 110 | #if 0 // Yikes! Can't include windows.h. |
| 111 | #if LLVM_ON_WIN32 |
| 112 | #define WIN32_LEAN_AND_MEAN 1 |
| 113 | #include <windows.h> |
| 114 | #endif |
| 115 | |
| 116 | // Read Windows registry string. |
| 117 | bool getWindowsRegistryString(const char *keyPath, const char *valueName, |
| 118 | char *value, size_t maxLength) { |
| 119 | HKEY hRootKey = NULL; |
| 120 | HKEY hKey = NULL; |
| 121 | const char* subKey = NULL; |
| 122 | DWORD valueType; |
| 123 | DWORD valueSize = maxLength - 1; |
| 124 | bool returnValue = false; |
| 125 | if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) { |
| 126 | hRootKey = HKEY_CLASSES_ROOT; |
| 127 | subKey = keyPath + 18; |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 128 | } |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 129 | else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) { |
| 130 | hRootKey = HKEY_USERS; |
| 131 | subKey = keyPath + 11; |
| 132 | } |
| 133 | else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) { |
| 134 | hRootKey = HKEY_LOCAL_MACHINE; |
| 135 | subKey = keyPath + 19; |
| 136 | } |
| 137 | else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) { |
| 138 | hRootKey = HKEY_CURRENT_USER; |
| 139 | subKey = keyPath + 18; |
| 140 | } |
| 141 | else |
| 142 | return(false); |
| 143 | long lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey); |
| 144 | if (lResult == ERROR_SUCCESS) { |
| 145 | lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType, (LPBYTE)value, |
| 146 | &valueSize); |
| 147 | if (lResult == ERROR_SUCCESS) |
| 148 | returnValue = true; |
| 149 | RegCloseKey(kKey); |
| 150 | } |
| 151 | return(returnValue); |
| 152 | } |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 153 | |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 154 | // Get Visual Studio installation directory. |
| 155 | bool getVisualStudioDir(std::string &path) { |
| 156 | char vs80comntools[256]; |
| 157 | char vs90comntools[256]; |
| 158 | const char* vscomntools = NULL; |
| 159 | bool has80 = getWindowsRegistryString( |
| 160 | "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0", |
| 161 | "InstallDir", vs80comntools, sizeof(vs80comntools) - 1); |
| 162 | bool has90 = getWindowsRegistryString( |
| 163 | "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0", |
| 164 | "InstallDir", vs90comntools, sizeof(vs90comntools) - 1); |
| 165 | // If we have both vc80 and vc90, pick version we were compiled with. |
| 166 | if (has80 && has90) { |
| 167 | #ifdef _MSC_VER |
| 168 | #if (_MSC_VER >= 1500) // VC90 |
| 169 | vscomntools = vs90comntools; |
| 170 | #elif (_MSC_VER == 1400) // VC80 |
| 171 | vscomntools = vs80comntools; |
| 172 | #else |
| 173 | vscomntools = vs90comntools; |
| 174 | #endif |
| 175 | #else |
| 176 | vscomntools = vs90comntools; |
| 177 | #endif |
| 178 | } |
| 179 | else if (has90) |
| 180 | vscomntools = vs90comntools; |
| 181 | else if (has80) |
| 182 | vscomntools = vs80comntools; |
| 183 | else |
| 184 | return(false); |
| 185 | char *p = strstr(vscomntools, "\\Common7\\ide"); |
| 186 | if (p) |
| 187 | *p = '\0'; |
| 188 | path = vscomntools; |
| 189 | return(true); |
| 190 | } |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 191 | #else |
| 192 | |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 193 | // Get Visual Studio installation directory. |
| 194 | bool getVisualStudioDir(std::string &path) { |
| 195 | const char* vs90comntools = getenv("VS90COMNTOOLS"); |
| 196 | const char* vs80comntools = getenv("VS80COMNTOOLS"); |
| 197 | const char* vscomntools = NULL; |
| 198 | // If we have both vc80 and vc90, pick version we were compiled with. |
| 199 | if (vs90comntools && vs80comntools) { |
| 200 | #if (_MSC_VER >= 1500) // VC90 |
| 201 | vscomntools = vs90comntools; |
| 202 | #elif (_MSC_VER == 1400) // VC80 |
| 203 | vscomntools = vs80comntools; |
| 204 | #else |
| 205 | vscomntools = vs90comntools; |
| 206 | #endif |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 207 | } |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 208 | else if (vs90comntools) |
| 209 | vscomntools = vs90comntools; |
| 210 | else if (vs80comntools) |
| 211 | vscomntools = vs80comntools; |
| 212 | else |
| 213 | return(false); |
| 214 | char *p = (char*)strstr(vscomntools, "\\Common7\\Tools"); |
| 215 | if (p) |
| 216 | *p = '\0'; |
| 217 | path = vscomntools; |
| 218 | return(true); |
| 219 | } |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 220 | #endif |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 221 | |
| 222 | #endif // LLVM_ON_WIN32 |
| 223 | |
| 224 | void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang, |
Mike Stump | e85c74d | 2009-10-09 19:42:16 +0000 | [diff] [blame] | 225 | const llvm::Triple &triple) { |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 226 | // FIXME: temporary hack: hard-coded paths. |
| 227 | llvm::Triple::OSType os = triple.getOS(); |
| 228 | |
| 229 | switch (os) { |
| 230 | case llvm::Triple::Win32: |
| 231 | { |
| 232 | #if defined(_MSC_VER) |
| 233 | std::string VSDir; |
| 234 | if (getVisualStudioDir(VSDir)) { |
| 235 | VSDir += "\\VC\\include"; |
| 236 | AddPath(VSDir, System, false, false, false); |
| 237 | } |
| 238 | else { |
| 239 | // Default install paths. |
| 240 | AddPath("C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\include", |
| 241 | System, false, false, false); |
| 242 | AddPath("C:\\Program Files\\Microsoft Visual Studio 8\\VC\\include", |
| 243 | System, false, false, false); |
| 244 | // For some clang developers. |
| 245 | AddPath("G:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\include", |
| 246 | System, false, false, false); |
| 247 | } |
| 248 | #else |
| 249 | // Default install paths. |
| 250 | AddPath("/Program Files/Microsoft Visual Studio 9.0/VC/include", |
| 251 | System, false, false, false); |
| 252 | AddPath("/Program Files/Microsoft Visual Studio 8/VC/include", |
| 253 | System, false, false, false); |
| 254 | #endif |
| 255 | } |
| 256 | break; |
| 257 | case llvm::Triple::Cygwin: |
| 258 | if (Lang.CPlusPlus) { |
| 259 | AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include", System, false, false, |
| 260 | false); |
| 261 | AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++", System, false, false, |
| 262 | false); |
| 263 | } |
| 264 | AddPath("/usr/include", System, false, false, false); |
| 265 | break; |
| 266 | case llvm::Triple::MinGW32: |
| 267 | case llvm::Triple::MinGW64: |
| 268 | if (Lang.CPlusPlus) { |
| 269 | // Try gcc 4.4.0 |
Mike Stump | fb16c0a | 2009-10-08 23:57:53 +0000 | [diff] [blame] | 270 | // FIXME: This can just use AddGnuCPlusPlusIncludePaths, right? |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 271 | AddPath("c:/mingw/lib/gcc/mingw32/4.4.0/include/c++", |
| 272 | System, true, false, false); |
| 273 | AddPath("c:/mingw/lib/gcc/mingw32/4.4.0/include/c++/mingw32", |
| 274 | System, true, false, false); |
| 275 | AddPath("c:/mingw/lib/gcc/mingw32/4.4.0/include/c++/backward", |
| 276 | System, true, false, false); |
| 277 | // Try gcc 4.3.0 |
Mike Stump | fb16c0a | 2009-10-08 23:57:53 +0000 | [diff] [blame] | 278 | // FIXME: This can just use AddGnuCPlusPlusIncludePaths, right? |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 279 | AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++", |
| 280 | System, true, false, false); |
| 281 | AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/mingw32", |
| 282 | System, true, false, false); |
| 283 | AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/backward", |
| 284 | System, true, false, false); |
| 285 | } |
| 286 | AddPath("c:/mingw/include", System, true, false, false); |
| 287 | break; |
| 288 | default: |
| 289 | if (Lang.CPlusPlus) { |
| 290 | switch (os) { |
| 291 | case llvm::Triple::Darwin: |
| 292 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", |
| 293 | "i686-apple-darwin10"); |
| 294 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", |
| 295 | "i686-apple-darwin8"); |
| 296 | break; |
| 297 | case llvm::Triple::Linux: |
| 298 | // Ubuntu 7.10 - Gutsy Gibbon |
| 299 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3", |
| 300 | "i486-linux-gnu"); |
| 301 | // Ubuntu 9.04 |
| 302 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3", |
| 303 | "x86_64-linux-gnu"); |
| 304 | // Fedora 8 |
| 305 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2", |
| 306 | "i386-redhat-linux"); |
| 307 | // Fedora 9 |
| 308 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0", |
| 309 | "i386-redhat-linux"); |
| 310 | // Fedora 10 |
| 311 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2", |
| 312 | "i386-redhat-linux"); |
| 313 | // openSUSE 11.1 |
| 314 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", |
| 315 | "i586-suse-linux"); |
| 316 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", |
| 317 | "x86_64-suse-linux"); |
| 318 | // openSUSE 11.2 |
| 319 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", |
| 320 | "i586-suse-linux"); |
| 321 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", |
| 322 | "x86_64-suse-linux"); |
| 323 | // Arch Linux 2008-06-24 |
| 324 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", |
| 325 | "i686-pc-linux-gnu"); |
| 326 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", |
| 327 | "x86_64-unknown-linux-gnu"); |
| 328 | // Gentoo x86 2009.0 stable |
| 329 | AddGnuCPlusPlusIncludePaths( |
| 330 | "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4", |
| 331 | "i686-pc-linux-gnu"); |
| 332 | // Gentoo x86 2008.0 stable |
| 333 | AddGnuCPlusPlusIncludePaths( |
| 334 | "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4", |
| 335 | "i686-pc-linux-gnu"); |
| 336 | // Ubuntu 8.10 |
| 337 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", |
| 338 | "i486-pc-linux-gnu"); |
| 339 | // Gentoo amd64 stable |
| 340 | AddGnuCPlusPlusIncludePaths( |
| 341 | "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4", |
| 342 | "i686-pc-linux-gnu"); |
| 343 | break; |
| 344 | case llvm::Triple::FreeBSD: |
| 345 | // DragonFly |
| 346 | AddPath("/usr/include/c++/4.1", System, true, false, false); |
| 347 | // FreeBSD |
| 348 | AddPath("/usr/include/c++/4.2", System, true, false, false); |
| 349 | break; |
| 350 | case llvm::Triple::Solaris: |
| 351 | // AuroraUX |
| 352 | AddGnuCPlusPlusIncludePaths("/Opt/gcc4/include/c++/4.2.4", |
| 353 | "i386-pc-solaris2.11"); |
| 354 | break; |
| 355 | default: |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | AddPath("/usr/local/include", System, false, false, false); |
| 361 | |
| 362 | AddPath("/usr/include", System, false, false, false); |
| 363 | AddPath("/System/Library/Frameworks", System, true, false, true); |
| 364 | AddPath("/Library/Frameworks", System, true, false, true); |
| 365 | break; |
| 366 | } |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) { |
| 370 | AddEnvVarPaths("CPATH"); |
| 371 | if (Lang.CPlusPlus && Lang.ObjC1) |
| 372 | AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH"); |
| 373 | else if (Lang.CPlusPlus) |
| 374 | AddEnvVarPaths("CPLUS_INCLUDE_PATH"); |
| 375 | else if (Lang.ObjC1) |
| 376 | AddEnvVarPaths("OBJC_INCLUDE_PATH"); |
| 377 | else |
| 378 | AddEnvVarPaths("C_INCLUDE_PATH"); |
| 379 | } |
| 380 | |
| 381 | |
| 382 | /// RemoveDuplicates - If there are duplicate directory entries in the specified |
| 383 | /// search list, remove the later (dead) ones. |
| 384 | static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList, |
| 385 | bool Verbose) { |
| 386 | llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; |
| 387 | llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; |
| 388 | llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; |
| 389 | for (unsigned i = 0; i != SearchList.size(); ++i) { |
Chris Lattner | 7a73940 | 2008-09-26 17:46:45 +0000 | [diff] [blame] | 390 | unsigned DirToRemove = i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 392 | const DirectoryLookup &CurEntry = SearchList[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 394 | if (CurEntry.isNormalDir()) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 395 | // 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] | 396 | if (SeenDirs.insert(CurEntry.getDir())) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 397 | continue; |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 398 | } else if (CurEntry.isFramework()) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 399 | // 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] | 400 | if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir())) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 401 | continue; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 402 | } else { |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 403 | assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 404 | // 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] | 405 | if (SeenHeaderMaps.insert(CurEntry.getHeaderMap())) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 406 | continue; |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 409 | // If we have a normal #include dir/framework/headermap that is shadowed |
| 410 | // later in the chain by a system include location, we actually want to |
| 411 | // ignore the user's request and drop the user dir... keeping the system |
| 412 | // dir. This is weird, but required to emulate GCC's search path correctly. |
| 413 | // |
| 414 | // Since dupes of system dirs are rare, just rescan to find the original |
| 415 | // that we're nuking instead of using a DenseMap. |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 416 | if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) { |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 417 | // Find the dir that this is the same of. |
| 418 | unsigned FirstDir; |
| 419 | for (FirstDir = 0; ; ++FirstDir) { |
| 420 | assert(FirstDir != i && "Didn't find dupe?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 422 | const DirectoryLookup &SearchEntry = SearchList[FirstDir]; |
| 423 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 424 | // 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] | 425 | if (SearchEntry.getLookupType() != CurEntry.getLookupType()) |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 426 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 428 | bool isSame; |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 429 | if (CurEntry.isNormalDir()) |
| 430 | isSame = SearchEntry.getDir() == CurEntry.getDir(); |
| 431 | else if (CurEntry.isFramework()) |
| 432 | isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir(); |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 433 | else { |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 434 | assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); |
| 435 | isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap(); |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 436 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 438 | if (isSame) |
| 439 | break; |
| 440 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 442 | // If the first dir in the search path is a non-system dir, zap it |
| 443 | // instead of the system one. |
| 444 | if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User) |
| 445 | DirToRemove = FirstDir; |
| 446 | } |
| 447 | |
| 448 | if (Verbose) { |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 449 | fprintf(stderr, "ignoring duplicate directory \"%s\"\n", |
| 450 | CurEntry.getName()); |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 451 | if (DirToRemove != i) |
| 452 | fprintf(stderr, " as it is a non-system directory that duplicates" |
| 453 | " a system directory\n"); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 7a73940 | 2008-09-26 17:46:45 +0000 | [diff] [blame] | 456 | // This is reached if the current entry is a duplicate. Remove the |
| 457 | // DirToRemove (usually the current dir). |
| 458 | SearchList.erase(SearchList.begin()+DirToRemove); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 459 | --i; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | |
| 464 | void InitHeaderSearch::Realize() { |
| 465 | // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. |
| 466 | std::vector<DirectoryLookup> SearchList; |
| 467 | SearchList = IncludeGroup[Angled]; |
| 468 | SearchList.insert(SearchList.end(), IncludeGroup[System].begin(), |
| 469 | IncludeGroup[System].end()); |
| 470 | SearchList.insert(SearchList.end(), IncludeGroup[After].begin(), |
| 471 | IncludeGroup[After].end()); |
| 472 | RemoveDuplicates(SearchList, Verbose); |
| 473 | RemoveDuplicates(IncludeGroup[Quoted], Verbose); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 475 | // Prepend QUOTED list on the search list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(), |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 477 | IncludeGroup[Quoted].end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 479 | |
| 480 | bool DontSearchCurDir = false; // TODO: set to true if -I- is set? |
| 481 | Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(), |
| 482 | DontSearchCurDir); |
| 483 | |
| 484 | // If verbose, print the list of directories that will be searched. |
| 485 | if (Verbose) { |
| 486 | fprintf(stderr, "#include \"...\" search starts here:\n"); |
| 487 | unsigned QuotedIdx = IncludeGroup[Quoted].size(); |
| 488 | for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { |
| 489 | if (i == QuotedIdx) |
| 490 | fprintf(stderr, "#include <...> search starts here:\n"); |
| 491 | const char *Name = SearchList[i].getName(); |
| 492 | const char *Suffix; |
| 493 | if (SearchList[i].isNormalDir()) |
| 494 | Suffix = ""; |
| 495 | else if (SearchList[i].isFramework()) |
| 496 | Suffix = " (framework directory)"; |
| 497 | else { |
| 498 | assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup"); |
| 499 | Suffix = " (headermap)"; |
| 500 | } |
| 501 | fprintf(stderr, " %s%s\n", Name, Suffix); |
| 502 | } |
| 503 | fprintf(stderr, "End of search list.\n"); |
| 504 | } |
| 505 | } |