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