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 | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/Utils.h" |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/LangOptions.h" |
Daniel Dunbar | 63c8b77 | 2009-11-07 04:20:50 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/HeaderSearchOptions.h" |
| 18 | #include "clang/Lex/HeaderSearch.h" |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
Rafael Espindola | aadd7a4 | 2009-11-13 05:13:58 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
Rafael Espindola | f0a2f51 | 2009-11-12 05:48:41 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Triple.h" |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 25 | #include "llvm/System/Path.h" |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 26 | #include "llvm/Config/config.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 27 | #include <cstdio> |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 28 | #ifdef _MSC_VER |
| 29 | #define WIN32_LEAN_AND_MEAN 1 |
| 30 | #include <windows.h> |
| 31 | #endif |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 32 | using namespace clang; |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame] | 33 | using namespace clang::frontend; |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | /// InitHeaderSearch - This class makes it easier to set the search paths of |
| 38 | /// a HeaderSearch object. InitHeaderSearch stores several search path lists |
| 39 | /// internally, which can be sent to a HeaderSearch object in one swoop. |
| 40 | class InitHeaderSearch { |
| 41 | std::vector<DirectoryLookup> IncludeGroup[4]; |
| 42 | HeaderSearch& Headers; |
| 43 | bool Verbose; |
| 44 | std::string isysroot; |
| 45 | |
| 46 | public: |
| 47 | |
| 48 | InitHeaderSearch(HeaderSearch &HS, |
| 49 | bool verbose = false, const std::string &iSysroot = "") |
| 50 | : Headers(HS), Verbose(verbose), isysroot(iSysroot) {} |
| 51 | |
| 52 | /// AddPath - Add the specified path to the specified group list. |
| 53 | void AddPath(const llvm::StringRef &Path, IncludeDirGroup Group, |
| 54 | bool isCXXAware, bool isUserSupplied, |
| 55 | bool isFramework, bool IgnoreSysRoot = false); |
| 56 | |
| 57 | /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to suport a gnu |
| 58 | /// libstdc++. |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 59 | void AddGnuCPlusPlusIncludePaths(const std::string &Base, |
| 60 | const char *ArchDir, |
| 61 | const char *Dir32, |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame] | 62 | const char *Dir64, |
| 63 | const llvm::Triple &triple); |
| 64 | |
| 65 | /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW |
| 66 | /// libstdc++. |
| 67 | void AddMinGWCPlusPlusIncludePaths(const std::string &Base, |
| 68 | const char *Arch, |
| 69 | const char *Version); |
| 70 | |
| 71 | /// AddDelimitedPaths - Add a list of paths delimited by the system PATH |
| 72 | /// separator. The processing follows that of the CPATH variable for gcc. |
| 73 | void AddDelimitedPaths(const char *String); |
| 74 | |
| 75 | // AddDefaultCIncludePaths - Add paths that should always be searched. |
| 76 | void AddDefaultCIncludePaths(const llvm::Triple &triple); |
| 77 | |
| 78 | // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when |
| 79 | // compiling c++. |
| 80 | void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple); |
| 81 | |
| 82 | /// AddDefaultSystemIncludePaths - Adds the default system include paths so |
| 83 | /// that e.g. stdio.h is found. |
| 84 | void AddDefaultSystemIncludePaths(const LangOptions &Lang, |
| 85 | const llvm::Triple &triple); |
| 86 | |
| 87 | /// Realize - Merges all search path lists into one list and send it to |
| 88 | /// HeaderSearch. |
| 89 | void Realize(); |
| 90 | }; |
| 91 | |
| 92 | } |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 93 | |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 94 | void InitHeaderSearch::AddPath(const llvm::StringRef &Path, |
| 95 | IncludeDirGroup Group, bool isCXXAware, |
| 96 | bool isUserSupplied, bool isFramework, |
| 97 | bool IgnoreSysRoot) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 98 | assert(!Path.empty() && "can't handle empty path here"); |
| 99 | FileManager &FM = Headers.getFileMgr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 101 | // Compute the actual path, taking into consideration -isysroot. |
| 102 | llvm::SmallString<256> MappedPath; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 104 | // Handle isysroot. |
Chris Lattner | 6858dd3 | 2009-02-19 06:48:28 +0000 | [diff] [blame] | 105 | if (Group == System && !IgnoreSysRoot) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 106 | // FIXME: Portability. This should be a sys::Path interface, this doesn't |
| 107 | // handle things like C:\ right, nor win32 \\network\device\blah. |
| 108 | if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present. |
| 109 | MappedPath.append(isysroot.begin(), isysroot.end()); |
| 110 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 112 | MappedPath.append(Path.begin(), Path.end()); |
| 113 | |
| 114 | // Compute the DirectoryLookup type. |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 115 | SrcMgr::CharacteristicKind Type; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 116 | if (Group == Quoted || Group == Angled) |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 117 | Type = SrcMgr::C_User; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 118 | else if (isCXXAware) |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 119 | Type = SrcMgr::C_System; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 120 | else |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 121 | Type = SrcMgr::C_ExternCSystem; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
| 123 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 124 | // If the directory exists, add it. |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 125 | if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 126 | IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied, |
| 127 | isFramework)); |
| 128 | return; |
| 129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 131 | // Check to see if this is an apple-style headermap (which are not allowed to |
| 132 | // be frameworks). |
| 133 | if (!isFramework) { |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 134 | if (const FileEntry *FE = FM.getFile(MappedPath.str())) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 135 | if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) { |
| 136 | // It is a headermap, add it to the search path. |
| 137 | IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied)); |
| 138 | return; |
| 139 | } |
| 140 | } |
| 141 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 143 | if (Verbose) |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 144 | llvm::errs() << "ignoring nonexistent directory \"" |
| 145 | << MappedPath.str() << "\"\n"; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
Daniel Dunbar | e166582 | 2009-11-07 04:20:39 +0000 | [diff] [blame] | 149 | void InitHeaderSearch::AddDelimitedPaths(const char *at) { |
| 150 | if (*at == 0) // Empty string should not add '.' path. |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 151 | return; |
| 152 | |
| 153 | const char* delim = strchr(at, llvm::sys::PathSeparator); |
| 154 | while (delim != 0) { |
| 155 | if (delim-at == 0) |
| 156 | AddPath(".", Angled, false, true, false); |
| 157 | else |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 158 | AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 159 | at = delim + 1; |
| 160 | delim = strchr(at, llvm::sys::PathSeparator); |
| 161 | } |
| 162 | if (*at == 0) |
| 163 | AddPath(".", Angled, false, true, false); |
| 164 | else |
| 165 | AddPath(at, Angled, false, true, false); |
| 166 | } |
| 167 | |
Mike Stump | ec05766 | 2009-10-09 20:16:49 +0000 | [diff] [blame] | 168 | void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(const std::string &Base, |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 169 | const char *ArchDir, |
Rafael Espindola | 31b63be | 2009-10-14 17:09:44 +0000 | [diff] [blame] | 170 | const char *Dir32, |
| 171 | const char *Dir64, |
| 172 | const llvm::Triple &triple) { |
Rafael Espindola | 6ec18a3 | 2009-11-23 16:31:19 +0000 | [diff] [blame] | 173 | // Add the base dir |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 174 | AddPath(Base, System, true, false, false); |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 175 | |
| 176 | // Add the multilib dirs |
Rafael Espindola | 31b63be | 2009-10-14 17:09:44 +0000 | [diff] [blame] | 177 | llvm::Triple::ArchType arch = triple.getArch(); |
| 178 | bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64; |
Rafael Espindola | 31b63be | 2009-10-14 17:09:44 +0000 | [diff] [blame] | 179 | if (is64bit) |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 180 | AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false); |
Rafael Espindola | 31b63be | 2009-10-14 17:09:44 +0000 | [diff] [blame] | 181 | else |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 182 | AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false); |
Rafael Espindola | 6ec18a3 | 2009-11-23 16:31:19 +0000 | [diff] [blame] | 183 | |
| 184 | // Add the backward dir |
| 185 | AddPath(Base + "/backward", System, true, false, false); |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 186 | } |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 187 | |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 188 | void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(const std::string &Base, |
| 189 | const char *Arch, |
| 190 | const char *Version) { |
Daniel Dunbar | 5c5758b | 2009-11-07 04:20:25 +0000 | [diff] [blame] | 191 | std::string localBase = Base + "/" + Arch + "/" + Version + "/include"; |
| 192 | AddPath(localBase, System, true, false, false); |
| 193 | AddPath(localBase + "/c++", System, true, false, false); |
| 194 | AddPath(localBase + "/c++/backward", System, true, false, false); |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 195 | } |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 196 | |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 197 | // FIXME: This probably should goto to some platform utils place. |
| 198 | #ifdef _MSC_VER |
John Thompson | 75ee3bd | 2009-11-21 00:15:52 +0000 | [diff] [blame] | 199 | |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 200 | // Read registry string. |
John Thompson | 75ee3bd | 2009-11-21 00:15:52 +0000 | [diff] [blame] | 201 | // This also supports a means to look for high-versioned keys by use |
| 202 | // of a $VERSION placeholder in the key path. |
| 203 | // $VERSION in the key path is a placeholder for the version number, |
| 204 | // causing the highest value path to be searched for and used. |
| 205 | // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION". |
| 206 | // There can be additional characters in the component. Only the numberic |
| 207 | // characters are compared. |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 208 | bool getSystemRegistryString(const char *keyPath, const char *valueName, |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 209 | char *value, size_t maxLength) { |
| 210 | HKEY hRootKey = NULL; |
| 211 | HKEY hKey = NULL; |
| 212 | const char* subKey = NULL; |
| 213 | DWORD valueType; |
| 214 | DWORD valueSize = maxLength - 1; |
John Thompson | 75ee3bd | 2009-11-21 00:15:52 +0000 | [diff] [blame] | 215 | long lResult; |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 216 | bool returnValue = false; |
| 217 | if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) { |
| 218 | hRootKey = HKEY_CLASSES_ROOT; |
| 219 | subKey = keyPath + 18; |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 220 | } |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 221 | else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) { |
| 222 | hRootKey = HKEY_USERS; |
| 223 | subKey = keyPath + 11; |
| 224 | } |
| 225 | else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) { |
| 226 | hRootKey = HKEY_LOCAL_MACHINE; |
| 227 | subKey = keyPath + 19; |
| 228 | } |
| 229 | else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) { |
| 230 | hRootKey = HKEY_CURRENT_USER; |
| 231 | subKey = keyPath + 18; |
| 232 | } |
| 233 | else |
| 234 | return(false); |
John Thompson | 75ee3bd | 2009-11-21 00:15:52 +0000 | [diff] [blame] | 235 | const char *placeHolder = strstr(subKey, "$VERSION"); |
| 236 | char bestName[256]; |
| 237 | bestName[0] = '\0'; |
| 238 | // If we have a $VERSION placeholder, do the highest-version search. |
| 239 | if (placeHolder) { |
| 240 | const char *keyEnd = placeHolder - 1; |
| 241 | const char *nextKey = placeHolder; |
| 242 | // Find end of previous key. |
| 243 | while ((keyEnd > subKey) && (*keyEnd != '\\')) |
| 244 | keyEnd--; |
| 245 | // Find end of key containing $VERSION. |
| 246 | while (*nextKey && (*nextKey != '\\')) |
| 247 | nextKey++; |
| 248 | size_t partialKeyLength = keyEnd - subKey; |
| 249 | char partialKey[256]; |
| 250 | if (partialKeyLength > sizeof(partialKey)) |
| 251 | partialKeyLength = sizeof(partialKey); |
| 252 | strncpy(partialKey, subKey, partialKeyLength); |
| 253 | partialKey[partialKeyLength] = '\0'; |
| 254 | HKEY hTopKey = NULL; |
| 255 | lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey); |
| 256 | if (lResult == ERROR_SUCCESS) { |
| 257 | char keyName[256]; |
| 258 | int bestIndex = -1; |
| 259 | double bestValue = 0.0; |
| 260 | DWORD index, size = sizeof(keyName) - 1; |
| 261 | for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
|
| 262 | NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
|
| 263 | const char *sp = keyName;
|
| 264 | while (*sp && !isdigit(*sp))
|
| 265 | sp++;
|
| 266 | if (!*sp)
|
| 267 | continue;
|
| 268 | const char *ep = sp + 1;
|
| 269 | while (*ep && (isdigit(*ep) || (*ep == '.')))
|
| 270 | ep++;
|
| 271 | char numBuf[32];
|
| 272 | strncpy(numBuf, sp, sizeof(numBuf) - 1);
|
| 273 | numBuf[sizeof(numBuf) - 1] = '\0';
|
| 274 | double value = strtod(numBuf, NULL);
|
| 275 | if (value > bestValue) {
|
| 276 | bestIndex = (int)index;
|
| 277 | bestValue = value;
|
| 278 | strcpy(bestName, keyName);
|
| 279 | }
|
| 280 | size = sizeof(keyName) - 1; |
| 281 | } |
| 282 | // If we found the highest versioned key, open the key and get the value. |
| 283 | if (bestIndex != -1) { |
| 284 | // Append rest of key. |
| 285 | strncat(bestName, nextKey, sizeof(bestName) - 1); |
| 286 | bestName[sizeof(bestName) - 1] = '\0'; |
| 287 | // Open the chosen key path remainder. |
| 288 | lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey); |
| 289 | if (lResult == ERROR_SUCCESS) { |
| 290 | lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType, |
| 291 | (LPBYTE)value, &valueSize); |
| 292 | if (lResult == ERROR_SUCCESS) |
| 293 | returnValue = true; |
| 294 | RegCloseKey(hKey); |
| 295 | } |
| 296 | } |
| 297 | RegCloseKey(hTopKey); |
| 298 | } |
| 299 | } |
| 300 | else { |
| 301 | lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey); |
| 302 | if (lResult == ERROR_SUCCESS) { |
| 303 | lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType, |
| 304 | (LPBYTE)value, &valueSize); |
| 305 | if (lResult == ERROR_SUCCESS) |
| 306 | returnValue = true; |
| 307 | RegCloseKey(hKey); |
| 308 | } |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 309 | } |
| 310 | return(returnValue); |
| 311 | } |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 312 | #else // _MSC_VER |
| 313 | // Read registry string. |
| 314 | bool getSystemRegistryString(const char *, const char *, char *, size_t) { |
| 315 | return(false); |
| 316 | } |
| 317 | #endif // _MSC_VER |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 318 | |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 319 | // Get Visual Studio installation directory. |
| 320 | bool getVisualStudioDir(std::string &path) { |
John Thompson | 75ee3bd | 2009-11-21 00:15:52 +0000 | [diff] [blame] | 321 | char vsIDEInstallDir[256]; |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 322 | // Try the Windows registry first. |
John Thompson | 75ee3bd | 2009-11-21 00:15:52 +0000 | [diff] [blame] | 323 | bool hasVCDir = getSystemRegistryString( |
| 324 | "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION", |
| 325 | "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1); |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 326 | // If we have both vc80 and vc90, pick version we were compiled with. |
John Thompson | 75ee3bd | 2009-11-21 00:15:52 +0000 | [diff] [blame] | 327 | if (hasVCDir && vsIDEInstallDir[0]) { |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 328 | char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE"); |
| 329 | if (p) |
| 330 | *p = '\0'; |
| 331 | path = vsIDEInstallDir; |
| 332 | return(true); |
| 333 | } |
| 334 | else { |
| 335 | // Try the environment. |
| 336 | const char* vs90comntools = getenv("VS90COMNTOOLS"); |
| 337 | const char* vs80comntools = getenv("VS80COMNTOOLS"); |
| 338 | const char* vscomntools = NULL; |
| 339 | // If we have both vc80 and vc90, pick version we were compiled with. |
| 340 | if (vs90comntools && vs80comntools) { |
| 341 | #if (_MSC_VER >= 1500) // VC90 |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 342 | vscomntools = vs90comntools; |
| 343 | #elif (_MSC_VER == 1400) // VC80 |
| 344 | vscomntools = vs80comntools; |
| 345 | #else |
| 346 | vscomntools = vs90comntools; |
| 347 | #endif |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 348 | } |
| 349 | else if (vs90comntools) |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 350 | vscomntools = vs90comntools; |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 351 | else if (vs80comntools) |
| 352 | vscomntools = vs80comntools; |
| 353 | if (vscomntools && *vscomntools) { |
| 354 | char *p = (char*)strstr(vscomntools, "\\Common7\\Tools"); |
| 355 | if (p) |
| 356 | *p = '\0'; |
| 357 | path = vscomntools; |
| 358 | return(true); |
| 359 | } |
| 360 | else |
| 361 | return(false); |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 362 | } |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 363 | return(false); |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 364 | } |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 365 | |
John Thompson | 75ee3bd | 2009-11-21 00:15:52 +0000 | [diff] [blame] | 366 | // Get Windows SDK installation directory. |
| 367 | bool getWindowsSDKDir(std::string &path) { |
| 368 | char windowsSDKInstallDir[256]; |
| 369 | // Try the Windows registry. |
| 370 | bool hasSDKDir = getSystemRegistryString( |
| 371 | "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION", |
| 372 | "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1); |
| 373 | // If we have both vc80 and vc90, pick version we were compiled with. |
| 374 | if (hasSDKDir && windowsSDKInstallDir[0]) { |
| 375 | path = windowsSDKInstallDir; |
| 376 | return(true); |
| 377 | } |
| 378 | return(false); |
| 379 | } |
| 380 | |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 381 | void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple) { |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 382 | // FIXME: temporary hack: hard-coded paths. |
Daniel Dunbar | c706468 | 2009-11-12 07:28:29 +0000 | [diff] [blame] | 383 | llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS); |
| 384 | if (CIncludeDirs != "") { |
Rafael Espindola | aadd7a4 | 2009-11-13 05:13:58 +0000 | [diff] [blame] | 385 | llvm::SmallVector<llvm::StringRef, 5> dirs; |
| 386 | CIncludeDirs.split(dirs, ":"); |
| 387 | for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin(); |
| 388 | i != dirs.end(); |
| 389 | ++i) |
Rafael Espindola | f0a2f51 | 2009-11-12 05:48:41 +0000 | [diff] [blame] | 390 | AddPath(*i, System, false, false, false); |
| 391 | return; |
| 392 | } |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 393 | llvm::Triple::OSType os = triple.getOS(); |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 394 | switch (os) { |
| 395 | case llvm::Triple::Win32: |
| 396 | { |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 397 | std::string VSDir; |
John Thompson | 75ee3bd | 2009-11-21 00:15:52 +0000 | [diff] [blame] | 398 | std::string WindowsSDKDir; |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 399 | if (getVisualStudioDir(VSDir)) { |
| 400 | AddPath(VSDir + "\\VC\\include", System, false, false, false); |
John Thompson | 75ee3bd | 2009-11-21 00:15:52 +0000 | [diff] [blame] | 401 | if (getWindowsSDKDir(WindowsSDKDir)) |
| 402 | AddPath(WindowsSDKDir, System, false, false, false); |
| 403 | else |
| 404 | AddPath(VSDir + "\\VC\\PlatformSDK\\Include", |
| 405 | System, false, false, false); |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 406 | } |
John Thompson | 9319f02 | 2009-11-23 17:49:27 +0000 | [diff] [blame] | 407 | else { |
| 408 | // Default install paths. |
| 409 | AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include", |
| 410 | System, false, false, false); |
| 411 | AddPath( |
| 412 | "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include", |
| 413 | System, false, false, false); |
| 414 | AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include", |
| 415 | System, false, false, false); |
| 416 | AddPath( |
| 417 | "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include", |
| 418 | System, false, false, false); |
| 419 | // For some clang developers. |
| 420 | AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include", |
| 421 | System, false, false, false); |
| 422 | AddPath( |
| 423 | "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include", |
| 424 | System, false, false, false); |
| 425 | } |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 426 | } |
| 427 | break; |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 428 | case llvm::Triple::MinGW64: |
Mike Stump | 620d57a | 2009-10-12 20:50:45 +0000 | [diff] [blame] | 429 | case llvm::Triple::MinGW32: |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 430 | AddPath("c:/mingw/include", System, true, false, false); |
| 431 | break; |
| 432 | default: |
Mike Stump | 43d8176 | 2009-10-08 23:29:47 +0000 | [diff] [blame] | 433 | break; |
| 434 | } |
John Thompson | d3f8834 | 2009-10-13 18:51:32 +0000 | [diff] [blame] | 435 | |
| 436 | AddPath("/usr/local/include", System, false, false, false); |
| 437 | AddPath("/usr/include", System, false, false, false); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) { |
| 441 | llvm::Triple::OSType os = triple.getOS(); |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 442 | llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT); |
| 443 | if (CxxIncludeRoot != "") { |
| 444 | llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH); |
| 445 | if (CxxIncludeArch == "") |
| 446 | AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(), |
| 447 | CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, triple); |
| 448 | else |
| 449 | AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH, |
| 450 | CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, triple); |
| 451 | return; |
| 452 | } |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 453 | // FIXME: temporary hack: hard-coded paths. |
| 454 | switch (os) { |
| 455 | case llvm::Triple::Cygwin: |
| 456 | AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include", |
| 457 | System, true, false, false); |
| 458 | AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++", |
| 459 | System, true, false, false); |
| 460 | break; |
| 461 | case llvm::Triple::MinGW64: |
| 462 | // Try gcc 4.4.0 |
| 463 | AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0"); |
| 464 | // Try gcc 4.3.0 |
| 465 | AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0"); |
| 466 | // Fall through. |
| 467 | case llvm::Triple::MinGW32: |
| 468 | // Try gcc 4.4.0 |
| 469 | AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0"); |
| 470 | // Try gcc 4.3.0 |
| 471 | AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0"); |
| 472 | break; |
| 473 | case llvm::Triple::Darwin: |
| 474 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 475 | "i686-apple-darwin10", "", "x86_64", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 476 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 477 | "i686-apple-darwin8", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 478 | break; |
| 479 | case llvm::Triple::Linux: |
| 480 | // Ubuntu 7.10 - Gutsy Gibbon |
| 481 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 482 | "i486-linux-gnu", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 483 | // Ubuntu 9.04 |
| 484 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 485 | "x86_64-linux-gnu","32", "", triple); |
Sebastian Redl | 5114eca | 2009-11-05 17:44:49 +0000 | [diff] [blame] | 486 | // Ubuntu 9.10 |
| 487 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 488 | "x86_64-linux-gnu", "32", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 489 | // Fedora 8 |
| 490 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 491 | "i386-redhat-linux", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 492 | // Fedora 9 |
| 493 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 494 | "i386-redhat-linux", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 495 | // Fedora 10 |
| 496 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 497 | "i386-redhat-linux","", "", triple); |
Nuno Lopes | 189a148 | 2009-11-17 15:28:35 +0000 | [diff] [blame] | 498 | |
| 499 | // Fedora 11 |
| 500 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1", |
| 501 | "i586-redhat-linux","", "", triple); |
| 502 | |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 503 | // openSUSE 11.1 32 bit |
| 504 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 505 | "i586-suse-linux", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 506 | // openSUSE 11.1 64 bit |
| 507 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 508 | "x86_64-suse-linux", "32", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 509 | // openSUSE 11.2 |
| 510 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 511 | "i586-suse-linux", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 512 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 513 | "x86_64-suse-linux", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 514 | // Arch Linux 2008-06-24 |
| 515 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 516 | "i686-pc-linux-gnu", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 517 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 518 | "x86_64-unknown-linux-gnu", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 519 | // Gentoo x86 2009.1 stable |
| 520 | AddGnuCPlusPlusIncludePaths( |
John Thompson | 40d1bb6 | 2009-11-05 22:03:02 +0000 | [diff] [blame] | 521 | "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 522 | "i686-pc-linux-gnu", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 523 | // Gentoo x86 2009.0 stable |
| 524 | AddGnuCPlusPlusIncludePaths( |
John Thompson | 40d1bb6 | 2009-11-05 22:03:02 +0000 | [diff] [blame] | 525 | "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 526 | "i686-pc-linux-gnu", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 527 | // Gentoo x86 2008.0 stable |
| 528 | AddGnuCPlusPlusIncludePaths( |
John Thompson | 40d1bb6 | 2009-11-05 22:03:02 +0000 | [diff] [blame] | 529 | "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 530 | "i686-pc-linux-gnu", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 531 | // Ubuntu 8.10 |
| 532 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 533 | "i486-pc-linux-gnu", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 534 | // Ubuntu 9.04 |
| 535 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 536 | "i486-linux-gnu","", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 537 | // Gentoo amd64 stable |
| 538 | AddGnuCPlusPlusIncludePaths( |
| 539 | "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 540 | "i686-pc-linux-gnu", "", "", triple); |
Benjamin Kramer | 5d7a188 | 2009-10-30 12:57:13 +0000 | [diff] [blame] | 541 | // Exherbo (2009-10-26) |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 542 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2", |
| 543 | "x86_64-pc-linux-gnu", "32", "", triple); |
| 544 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2", |
| 545 | "i686-pc-linux-gnu", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 546 | break; |
| 547 | case llvm::Triple::FreeBSD: |
| 548 | // DragonFly |
| 549 | AddPath("/usr/include/c++/4.1", System, true, false, false); |
| 550 | // FreeBSD |
| 551 | AddPath("/usr/include/c++/4.2", System, true, false, false); |
| 552 | break; |
| 553 | case llvm::Triple::Solaris: |
| 554 | // Solaris - Fall though.. |
| 555 | case llvm::Triple::AuroraUX: |
| 556 | // AuroraUX |
| 557 | AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4", |
Rafael Espindola | ab7ae95 | 2009-11-16 19:49:37 +0000 | [diff] [blame] | 558 | "i386-pc-solaris2.11", "", "", triple); |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 559 | break; |
| 560 | default: |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 565 | void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang, |
| 566 | const llvm::Triple &triple) { |
Rafael Espindola | 6ec18a3 | 2009-11-23 16:31:19 +0000 | [diff] [blame] | 567 | if (Lang.CPlusPlus) |
| 568 | AddDefaultCPlusPlusIncludePaths(triple); |
| 569 | |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 570 | AddDefaultCIncludePaths(triple); |
Daniel Dunbar | e166582 | 2009-11-07 04:20:39 +0000 | [diff] [blame] | 571 | |
| 572 | // Add the default framework include paths on Darwin. |
| 573 | if (triple.getOS() == llvm::Triple::Darwin) { |
| 574 | AddPath("/System/Library/Frameworks", System, true, false, true); |
| 575 | AddPath("/Library/Frameworks", System, true, false, true); |
| 576 | } |
Rafael Espindola | e4b255c | 2009-10-27 14:47:31 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 579 | /// RemoveDuplicates - If there are duplicate directory entries in the specified |
| 580 | /// search list, remove the later (dead) ones. |
| 581 | static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList, |
| 582 | bool Verbose) { |
| 583 | llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; |
| 584 | llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; |
| 585 | llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; |
| 586 | for (unsigned i = 0; i != SearchList.size(); ++i) { |
Chris Lattner | 7a73940 | 2008-09-26 17:46:45 +0000 | [diff] [blame] | 587 | unsigned DirToRemove = i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 589 | const DirectoryLookup &CurEntry = SearchList[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 591 | if (CurEntry.isNormalDir()) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 592 | // 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] | 593 | if (SeenDirs.insert(CurEntry.getDir())) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 594 | continue; |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 595 | } else if (CurEntry.isFramework()) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 596 | // 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] | 597 | if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir())) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 598 | continue; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 599 | } else { |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 600 | assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 601 | // 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] | 602 | if (SeenHeaderMaps.insert(CurEntry.getHeaderMap())) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 603 | continue; |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 604 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 606 | // If we have a normal #include dir/framework/headermap that is shadowed |
| 607 | // later in the chain by a system include location, we actually want to |
| 608 | // ignore the user's request and drop the user dir... keeping the system |
| 609 | // dir. This is weird, but required to emulate GCC's search path correctly. |
| 610 | // |
| 611 | // Since dupes of system dirs are rare, just rescan to find the original |
| 612 | // that we're nuking instead of using a DenseMap. |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 613 | if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) { |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 614 | // Find the dir that this is the same of. |
| 615 | unsigned FirstDir; |
| 616 | for (FirstDir = 0; ; ++FirstDir) { |
| 617 | assert(FirstDir != i && "Didn't find dupe?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 | |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 619 | const DirectoryLookup &SearchEntry = SearchList[FirstDir]; |
| 620 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 621 | // 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] | 622 | if (SearchEntry.getLookupType() != CurEntry.getLookupType()) |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 623 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 625 | bool isSame; |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 626 | if (CurEntry.isNormalDir()) |
| 627 | isSame = SearchEntry.getDir() == CurEntry.getDir(); |
| 628 | else if (CurEntry.isFramework()) |
| 629 | isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir(); |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 630 | else { |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 631 | assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); |
| 632 | isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap(); |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 633 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 635 | if (isSame) |
| 636 | break; |
| 637 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 639 | // If the first dir in the search path is a non-system dir, zap it |
| 640 | // instead of the system one. |
| 641 | if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User) |
| 642 | DirToRemove = FirstDir; |
| 643 | } |
| 644 | |
| 645 | if (Verbose) { |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 646 | fprintf(stderr, "ignoring duplicate directory \"%s\"\n", |
| 647 | CurEntry.getName()); |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 648 | if (DirToRemove != i) |
| 649 | fprintf(stderr, " as it is a non-system directory that duplicates" |
| 650 | " a system directory\n"); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 651 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 652 | |
Chris Lattner | 7a73940 | 2008-09-26 17:46:45 +0000 | [diff] [blame] | 653 | // This is reached if the current entry is a duplicate. Remove the |
| 654 | // DirToRemove (usually the current dir). |
| 655 | SearchList.erase(SearchList.begin()+DirToRemove); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 656 | --i; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | |
| 661 | void InitHeaderSearch::Realize() { |
| 662 | // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. |
| 663 | std::vector<DirectoryLookup> SearchList; |
| 664 | SearchList = IncludeGroup[Angled]; |
| 665 | SearchList.insert(SearchList.end(), IncludeGroup[System].begin(), |
| 666 | IncludeGroup[System].end()); |
| 667 | SearchList.insert(SearchList.end(), IncludeGroup[After].begin(), |
| 668 | IncludeGroup[After].end()); |
| 669 | RemoveDuplicates(SearchList, Verbose); |
| 670 | RemoveDuplicates(IncludeGroup[Quoted], Verbose); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 672 | // Prepend QUOTED list on the search list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(), |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 674 | IncludeGroup[Quoted].end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 676 | |
| 677 | bool DontSearchCurDir = false; // TODO: set to true if -I- is set? |
| 678 | Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(), |
| 679 | DontSearchCurDir); |
| 680 | |
| 681 | // If verbose, print the list of directories that will be searched. |
| 682 | if (Verbose) { |
| 683 | fprintf(stderr, "#include \"...\" search starts here:\n"); |
| 684 | unsigned QuotedIdx = IncludeGroup[Quoted].size(); |
| 685 | for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { |
| 686 | if (i == QuotedIdx) |
| 687 | fprintf(stderr, "#include <...> search starts here:\n"); |
| 688 | const char *Name = SearchList[i].getName(); |
| 689 | const char *Suffix; |
| 690 | if (SearchList[i].isNormalDir()) |
| 691 | Suffix = ""; |
| 692 | else if (SearchList[i].isFramework()) |
| 693 | Suffix = " (framework directory)"; |
| 694 | else { |
| 695 | assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup"); |
| 696 | Suffix = " (headermap)"; |
| 697 | } |
| 698 | fprintf(stderr, " %s%s\n", Name, Suffix); |
| 699 | } |
| 700 | fprintf(stderr, "End of search list.\n"); |
| 701 | } |
| 702 | } |
Daniel Dunbar | 63c8b77 | 2009-11-07 04:20:50 +0000 | [diff] [blame] | 703 | |
Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 704 | void clang::ApplyHeaderSearchOptions(HeaderSearch &HS, |
| 705 | const HeaderSearchOptions &HSOpts, |
| 706 | const LangOptions &Lang, |
Daniel Dunbar | 63c8b77 | 2009-11-07 04:20:50 +0000 | [diff] [blame] | 707 | const llvm::Triple &Triple) { |
| 708 | InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot); |
| 709 | |
| 710 | // Add the user defined entries. |
| 711 | for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) { |
| 712 | const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i]; |
Daniel Dunbar | 1b483e7 | 2009-11-17 05:04:15 +0000 | [diff] [blame] | 713 | Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework, |
| 714 | false); |
Daniel Dunbar | 63c8b77 | 2009-11-07 04:20:50 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | // Add entries from CPATH and friends. |
| 718 | Init.AddDelimitedPaths(HSOpts.EnvIncPath.c_str()); |
Daniel Dunbar | c363cb1 | 2009-11-16 22:38:40 +0000 | [diff] [blame] | 719 | if (Lang.CPlusPlus && Lang.ObjC1) |
| 720 | Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath.c_str()); |
| 721 | else if (Lang.CPlusPlus) |
| 722 | Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath.c_str()); |
| 723 | else if (Lang.ObjC1) |
| 724 | Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath.c_str()); |
| 725 | else |
| 726 | Init.AddDelimitedPaths(HSOpts.CEnvIncPath.c_str()); |
Daniel Dunbar | 63c8b77 | 2009-11-07 04:20:50 +0000 | [diff] [blame] | 727 | |
| 728 | if (!HSOpts.BuiltinIncludePath.empty()) { |
| 729 | // Ignore the sys root, we *always* look for clang headers relative to |
| 730 | // supplied path. |
Daniel Dunbar | 2cdafa8 | 2009-11-09 23:02:47 +0000 | [diff] [blame] | 731 | Init.AddPath(HSOpts.BuiltinIncludePath, System, |
Daniel Dunbar | 63c8b77 | 2009-11-07 04:20:50 +0000 | [diff] [blame] | 732 | false, false, false, /*IgnoreSysRoot=*/ true); |
| 733 | } |
| 734 | |
Daniel Dunbar | dd35ce9 | 2009-11-07 04:58:12 +0000 | [diff] [blame] | 735 | if (HSOpts.UseStandardIncludes) |
Daniel Dunbar | 63c8b77 | 2009-11-07 04:20:50 +0000 | [diff] [blame] | 736 | Init.AddDefaultSystemIncludePaths(Lang, Triple); |
| 737 | |
| 738 | Init.Realize(); |
| 739 | } |