blob: 76cda2c9b35c6d01be89a0a715b9f8251536e8e3 [file] [log] [blame]
Nick Lewyckydf22c2c2010-07-25 03:12:58 +00001//===--- InitHeaderSearch.cpp - Initialize header search paths ------------===//
Nico Weber0fca0222008-08-22 09:25:22 +00002//
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
Oscar Fuentes2100fe92011-02-03 22:48:20 +000014#ifdef HAVE_CLANG_CONFIG_H
15# include "clang/Config/config.h"
16#endif
17
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000018#include "clang/Frontend/Utils.h"
Nico Weber0fca0222008-08-22 09:25:22 +000019#include "clang/Basic/FileManager.h"
20#include "clang/Basic/LangOptions.h"
Daniel Dunbar63c8b772009-11-07 04:20:50 +000021#include "clang/Frontend/HeaderSearchOptions.h"
22#include "clang/Lex/HeaderSearch.h"
Nico Weber0fca0222008-08-22 09:25:22 +000023#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/SmallPtrSet.h"
Rafael Espindolaaadd7a42009-11-13 05:13:58 +000025#include "llvm/ADT/SmallVector.h"
Rafael Espindolaf0a2f512009-11-12 05:48:41 +000026#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000027#include "llvm/ADT/Triple.h"
Benjamin Kramere89ba592009-12-08 12:38:20 +000028#include "llvm/ADT/Twine.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000029#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000030#include "llvm/Support/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000031#include "llvm/Config/config.h"
Mike Stump620d57a2009-10-12 20:50:45 +000032#ifdef _MSC_VER
33 #define WIN32_LEAN_AND_MEAN 1
34 #include <windows.h>
35#endif
Nico Weber0fca0222008-08-22 09:25:22 +000036using namespace clang;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000037using namespace clang::frontend;
38
39namespace {
40
41/// InitHeaderSearch - This class makes it easier to set the search paths of
42/// a HeaderSearch object. InitHeaderSearch stores several search path lists
43/// internally, which can be sent to a HeaderSearch object in one swoop.
44class InitHeaderSearch {
Joerg Sonnenberger2df66472011-02-22 00:40:56 +000045 std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath;
46 typedef std::vector<std::pair<IncludeDirGroup,
47 DirectoryLookup> >::const_iterator path_iterator;
Chris Lattnerebb61642011-06-16 22:56:45 +000048 HeaderSearch &Headers;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000049 bool Verbose;
Michael J. Spenceraf6530c2010-12-25 20:09:27 +000050 std::string IncludeSysroot;
51 bool IsNotEmptyOrRoot;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000052
53public:
54
Chris Lattner5f9e2722011-07-23 10:55:15 +000055 InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
Michael J. Spenceraf6530c2010-12-25 20:09:27 +000056 : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
57 IsNotEmptyOrRoot(!(sysroot.empty() || sysroot == "/")) {
Chandler Carruthc09265a2010-11-15 07:15:26 +000058 }
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000059
60 /// AddPath - Add the specified path to the specified group list.
Chris Lattner5f9e2722011-07-23 10:55:15 +000061 void AddPath(const Twine &Path, IncludeDirGroup Group,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000062 bool isCXXAware, bool isUserSupplied,
63 bool isFramework, bool IgnoreSysRoot = false);
64
mike-ma6087372010-05-05 17:00:31 +000065 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000066 /// libstdc++.
Chris Lattner5f9e2722011-07-23 10:55:15 +000067 void AddGnuCPlusPlusIncludePaths(StringRef Base,
68 StringRef ArchDir,
69 StringRef Dir32,
70 StringRef Dir64,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000071 const llvm::Triple &triple);
72
Michael J. Spencer06a8dc62010-12-21 16:45:42 +000073 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000074 /// libstdc++.
Chris Lattner5f9e2722011-07-23 10:55:15 +000075 void AddMinGWCPlusPlusIncludePaths(StringRef Base,
76 StringRef Arch,
77 StringRef Version);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000078
NAKAMURA Takumi9db48462011-03-15 02:32:36 +000079 /// AddMinGW64CXXPaths - Add the necessary paths to support
80 /// libstdc++ of x86_64-w64-mingw32 aka mingw-w64.
Chris Lattner5f9e2722011-07-23 10:55:15 +000081 void AddMinGW64CXXPaths(StringRef Base,
82 StringRef Version);
NAKAMURA Takumi9db48462011-03-15 02:32:36 +000083
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000084 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-m79bc57c2010-05-16 19:03:52 +000085 void AddDefaultCIncludePaths(const llvm::Triple &triple,
86 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000087
88 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
89 // compiling c++.
Douglas Gregord944a9b2011-06-27 15:47:15 +000090 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple,
91 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000092
93 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
94 /// that e.g. stdio.h is found.
95 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +000096 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +000097 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000098
99 /// Realize - Merges all search path lists into one list and send it to
100 /// HeaderSearch.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000101 void Realize(const LangOptions &Lang);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000102};
103
Chris Lattnerebb61642011-06-16 22:56:45 +0000104} // end anonymous namespace.
Nico Weber0fca0222008-08-22 09:25:22 +0000105
Chris Lattner5f9e2722011-07-23 10:55:15 +0000106void InitHeaderSearch::AddPath(const Twine &Path,
Benjamin Kramer458fb102009-09-05 09:49:39 +0000107 IncludeDirGroup Group, bool isCXXAware,
108 bool isUserSupplied, bool isFramework,
109 bool IgnoreSysRoot) {
Benjamin Kramere89ba592009-12-08 12:38:20 +0000110 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
Nico Weber0fca0222008-08-22 09:25:22 +0000111 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Nico Weber0fca0222008-08-22 09:25:22 +0000113 // Compute the actual path, taking into consideration -isysroot.
Chandler Carruth5853b0f2010-11-15 00:05:18 +0000114 llvm::SmallString<256> MappedPathStorage;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000115 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Nico Weber0fca0222008-08-22 09:25:22 +0000117 // Handle isysroot.
Rafael Espindola9a7e09d2011-03-02 21:30:07 +0000118 if ((Group == System || Group == CXXSystem) && !IgnoreSysRoot &&
NAKAMURA Takumi0f0cdab2011-05-02 04:50:10 +0000119#if defined(_WIN32)
120 !MappedPathStr.empty() &&
121 llvm::sys::path::is_separator(MappedPathStr[0]) &&
122#else
Michael J. Spencer256053b2010-12-17 21:22:22 +0000123 llvm::sys::path::is_absolute(MappedPathStr) &&
NAKAMURA Takumi0f0cdab2011-05-02 04:50:10 +0000124#endif
Michael J. Spenceraf6530c2010-12-25 20:09:27 +0000125 IsNotEmptyOrRoot) {
Chandler Carruth5619ae52010-11-15 09:28:23 +0000126 MappedPathStorage.clear();
Chandler Carruthc09265a2010-11-15 07:15:26 +0000127 MappedPathStr =
Michael J. Spenceraf6530c2010-12-25 20:09:27 +0000128 (IncludeSysroot + Path).toStringRef(MappedPathStorage);
Nico Weber0fca0222008-08-22 09:25:22 +0000129 }
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Nico Weber0fca0222008-08-22 09:25:22 +0000131 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000132 SrcMgr::CharacteristicKind Type;
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000133 if (Group == Quoted || Group == Angled || Group == IndexHeaderMap)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000134 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000135 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000136 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000137 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000138 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000139
140
Nico Weber0fca0222008-08-22 09:25:22 +0000141 // If the directory exists, add it.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000142 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000143 IncludePath.push_back(std::make_pair(Group, DirectoryLookup(DE, Type,
144 isUserSupplied, isFramework)));
Nico Weber0fca0222008-08-22 09:25:22 +0000145 return;
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Nico Weber0fca0222008-08-22 09:25:22 +0000148 // Check to see if this is an apple-style headermap (which are not allowed to
149 // be frameworks).
150 if (!isFramework) {
Chris Lattner39b49bc2010-11-23 08:35:12 +0000151 if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000152 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
153 // It is a headermap, add it to the search path.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000154 IncludePath.push_back(std::make_pair(Group, DirectoryLookup(HM, Type,
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000155 isUserSupplied, Group == IndexHeaderMap)));
Nico Weber0fca0222008-08-22 09:25:22 +0000156 return;
157 }
158 }
159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Nico Weber0fca0222008-08-22 09:25:22 +0000161 if (Verbose)
Chandler Carruthf3721452010-11-15 00:48:13 +0000162 llvm::errs() << "ignoring nonexistent directory \""
163 << MappedPathStr << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000164}
165
Chris Lattner5f9e2722011-07-23 10:55:15 +0000166void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base,
167 StringRef ArchDir,
168 StringRef Dir32,
169 StringRef Dir64,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000170 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000171 // Add the base dir
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000172 AddPath(Base, CXXSystem, true, false, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000173
174 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000175 llvm::Triple::ArchType arch = triple.getArch();
176 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000177 if (is64bit)
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000178 AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000179 else
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000180 AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, true, false, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000181
182 // Add the backward dir
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000183 AddPath(Base + "/backward", CXXSystem, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000184}
Nico Weber0fca0222008-08-22 09:25:22 +0000185
Chris Lattner5f9e2722011-07-23 10:55:15 +0000186void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base,
187 StringRef Arch,
188 StringRef Version) {
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000189 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000190 CXXSystem, true, false, false);
Chris Lattner8e9006b2010-09-03 16:45:53 +0000191 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000192 CXXSystem, true, false, false);
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000193 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000194 CXXSystem, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000195}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000196
Chris Lattner5f9e2722011-07-23 10:55:15 +0000197void InitHeaderSearch::AddMinGW64CXXPaths(StringRef Base,
198 StringRef Version) {
Douglas Gregord944a9b2011-06-27 15:47:15 +0000199 // Assumes Base is HeaderSearchOpts' ResourceDir
200 AddPath(Base + "/../../../include/c++/" + Version,
NAKAMURA Takumi9db48462011-03-15 02:32:36 +0000201 CXXSystem, true, false, false);
Douglas Gregord944a9b2011-06-27 15:47:15 +0000202 AddPath(Base + "/../../../include/c++/" + Version + "/x86_64-w64-mingw32",
NAKAMURA Takumi9db48462011-03-15 02:32:36 +0000203 CXXSystem, true, false, false);
Douglas Gregord944a9b2011-06-27 15:47:15 +0000204 AddPath(Base + "/../../../include/c++/" + Version + "/i686-w64-mingw32",
205 CXXSystem, true, false, false);
206 AddPath(Base + "/../../../include/c++/" + Version + "/backward",
NAKAMURA Takumi9db48462011-03-15 02:32:36 +0000207 CXXSystem, true, false, false);
208}
209
Mike Stump620d57a2009-10-12 20:50:45 +0000210 // FIXME: This probably should goto to some platform utils place.
211#ifdef _MSC_VER
John Thompson75ee3bd2009-11-21 00:15:52 +0000212
Mike Stump620d57a2009-10-12 20:50:45 +0000213 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000214 // This also supports a means to look for high-versioned keys by use
215 // of a $VERSION placeholder in the key path.
216 // $VERSION in the key path is a placeholder for the version number,
217 // causing the highest value path to be searched for and used.
218 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
219 // There can be additional characters in the component. Only the numberic
220 // characters are compared.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000221static bool getSystemRegistryString(const char *keyPath, const char *valueName,
222 char *value, size_t maxLength) {
Mike Stump43d81762009-10-08 23:29:47 +0000223 HKEY hRootKey = NULL;
224 HKEY hKey = NULL;
225 const char* subKey = NULL;
226 DWORD valueType;
227 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000228 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000229 bool returnValue = false;
Chris Lattnerebb61642011-06-16 22:56:45 +0000230
Mike Stump43d81762009-10-08 23:29:47 +0000231 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
232 hRootKey = HKEY_CLASSES_ROOT;
233 subKey = keyPath + 18;
Chris Lattnerebb61642011-06-16 22:56:45 +0000234 } else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
Mike Stump43d81762009-10-08 23:29:47 +0000235 hRootKey = HKEY_USERS;
236 subKey = keyPath + 11;
Chris Lattnerebb61642011-06-16 22:56:45 +0000237 } else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
Mike Stump43d81762009-10-08 23:29:47 +0000238 hRootKey = HKEY_LOCAL_MACHINE;
239 subKey = keyPath + 19;
Chris Lattnerebb61642011-06-16 22:56:45 +0000240 } else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
Mike Stump43d81762009-10-08 23:29:47 +0000241 hRootKey = HKEY_CURRENT_USER;
242 subKey = keyPath + 18;
243 }
244 else
Chris Lattnerebb61642011-06-16 22:56:45 +0000245 return false;
246
John Thompson75ee3bd2009-11-21 00:15:52 +0000247 const char *placeHolder = strstr(subKey, "$VERSION");
248 char bestName[256];
249 bestName[0] = '\0';
250 // If we have a $VERSION placeholder, do the highest-version search.
251 if (placeHolder) {
252 const char *keyEnd = placeHolder - 1;
253 const char *nextKey = placeHolder;
254 // Find end of previous key.
255 while ((keyEnd > subKey) && (*keyEnd != '\\'))
256 keyEnd--;
257 // Find end of key containing $VERSION.
258 while (*nextKey && (*nextKey != '\\'))
259 nextKey++;
260 size_t partialKeyLength = keyEnd - subKey;
261 char partialKey[256];
262 if (partialKeyLength > sizeof(partialKey))
263 partialKeyLength = sizeof(partialKey);
264 strncpy(partialKey, subKey, partialKeyLength);
265 partialKey[partialKeyLength] = '\0';
266 HKEY hTopKey = NULL;
267 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
268 if (lResult == ERROR_SUCCESS) {
269 char keyName[256];
270 int bestIndex = -1;
271 double bestValue = 0.0;
272 DWORD index, size = sizeof(keyName) - 1;
Nuno Lopes33cc2432009-12-07 17:18:48 +0000273 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
274 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
275 const char *sp = keyName;
276 while (*sp && !isdigit(*sp))
277 sp++;
278 if (!*sp)
279 continue;
280 const char *ep = sp + 1;
281 while (*ep && (isdigit(*ep) || (*ep == '.')))
282 ep++;
283 char numBuf[32];
284 strncpy(numBuf, sp, sizeof(numBuf) - 1);
285 numBuf[sizeof(numBuf) - 1] = '\0';
286 double value = strtod(numBuf, NULL);
287 if (value > bestValue) {
288 bestIndex = (int)index;
289 bestValue = value;
290 strcpy(bestName, keyName);
291 }
John Thompson75ee3bd2009-11-21 00:15:52 +0000292 size = sizeof(keyName) - 1;
293 }
294 // If we found the highest versioned key, open the key and get the value.
295 if (bestIndex != -1) {
296 // Append rest of key.
297 strncat(bestName, nextKey, sizeof(bestName) - 1);
298 bestName[sizeof(bestName) - 1] = '\0';
299 // Open the chosen key path remainder.
300 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
301 if (lResult == ERROR_SUCCESS) {
302 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
303 (LPBYTE)value, &valueSize);
304 if (lResult == ERROR_SUCCESS)
305 returnValue = true;
306 RegCloseKey(hKey);
307 }
308 }
309 RegCloseKey(hTopKey);
310 }
311 }
312 else {
313 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
314 if (lResult == ERROR_SUCCESS) {
315 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
316 (LPBYTE)value, &valueSize);
317 if (lResult == ERROR_SUCCESS)
318 returnValue = true;
319 RegCloseKey(hKey);
320 }
Mike Stump43d81762009-10-08 23:29:47 +0000321 }
Chris Lattnerebb61642011-06-16 22:56:45 +0000322 return returnValue;
Mike Stump43d81762009-10-08 23:29:47 +0000323}
Mike Stump620d57a2009-10-12 20:50:45 +0000324#else // _MSC_VER
325 // Read registry string.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000326static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
Mike Stump620d57a2009-10-12 20:50:45 +0000327 return(false);
328}
329#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000330
Mike Stump43d81762009-10-08 23:29:47 +0000331 // Get Visual Studio installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000332static bool getVisualStudioDir(std::string &path) {
Michael J. Spencerff58e362010-08-21 21:55:07 +0000333 // First check the environment variables that vsvars32.bat sets.
334 const char* vcinstalldir = getenv("VCINSTALLDIR");
Chris Lattnerebb61642011-06-16 22:56:45 +0000335 if (vcinstalldir) {
Michael J. Spencerff58e362010-08-21 21:55:07 +0000336 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
337 if (p)
338 *p = '\0';
339 path = vcinstalldir;
Chris Lattnerebb61642011-06-16 22:56:45 +0000340 return true;
Michael J. Spencerff58e362010-08-21 21:55:07 +0000341 }
342
John Thompson75ee3bd2009-11-21 00:15:52 +0000343 char vsIDEInstallDir[256];
Douglas Gregor80f93d92010-05-18 05:47:04 +0000344 char vsExpressIDEInstallDir[256];
Michael J. Spencerff58e362010-08-21 21:55:07 +0000345 // Then try the windows registry.
John Thompson75ee3bd2009-11-21 00:15:52 +0000346 bool hasVCDir = getSystemRegistryString(
347 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
348 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Douglas Gregor80f93d92010-05-18 05:47:04 +0000349 bool hasVCExpressDir = getSystemRegistryString(
350 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
351 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000352 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000353 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000354 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
355 if (p)
356 *p = '\0';
357 path = vsIDEInstallDir;
Chris Lattnerebb61642011-06-16 22:56:45 +0000358 return true;
Mike Stump620d57a2009-10-12 20:50:45 +0000359 }
Chris Lattnerebb61642011-06-16 22:56:45 +0000360
361 if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
Douglas Gregor80f93d92010-05-18 05:47:04 +0000362 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
363 if (p)
364 *p = '\0';
365 path = vsExpressIDEInstallDir;
Chris Lattnerebb61642011-06-16 22:56:45 +0000366 return true;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000367 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000368
Chris Lattnerebb61642011-06-16 22:56:45 +0000369 // Try the environment.
370 const char *vs100comntools = getenv("VS100COMNTOOLS");
371 const char *vs90comntools = getenv("VS90COMNTOOLS");
372 const char *vs80comntools = getenv("VS80COMNTOOLS");
373 const char *vscomntools = NULL;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000374
Chris Lattnerebb61642011-06-16 22:56:45 +0000375 // Try to find the version that we were compiled with
376 if(false) {}
377 #if (_MSC_VER >= 1600) // VC100
378 else if(vs100comntools) {
379 vscomntools = vs100comntools;
Mike Stump43d81762009-10-08 23:29:47 +0000380 }
Chris Lattnerebb61642011-06-16 22:56:45 +0000381 #elif (_MSC_VER == 1500) // VC80
382 else if(vs90comntools) {
383 vscomntools = vs90comntools;
384 }
385 #elif (_MSC_VER == 1400) // VC80
386 else if(vs80comntools) {
387 vscomntools = vs80comntools;
388 }
389 #endif
390 // Otherwise find any version we can
391 else if (vs100comntools)
392 vscomntools = vs100comntools;
393 else if (vs90comntools)
394 vscomntools = vs90comntools;
395 else if (vs80comntools)
396 vscomntools = vs80comntools;
397
398 if (vscomntools && *vscomntools) {
Francois Picheta6c2b3a2011-07-16 21:17:14 +0000399 const char *p = strstr(vscomntools, "\\Common7\\Tools");
400 path = p ? std::string(vscomntools, p) : vscomntools;
Chris Lattnerebb61642011-06-16 22:56:45 +0000401 return true;
402 }
403 return false;
Mike Stump43d81762009-10-08 23:29:47 +0000404}
Mike Stump43d81762009-10-08 23:29:47 +0000405
John Thompson75ee3bd2009-11-21 00:15:52 +0000406 // Get Windows SDK installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000407static bool getWindowsSDKDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000408 char windowsSDKInstallDir[256];
409 // Try the Windows registry.
410 bool hasSDKDir = getSystemRegistryString(
411 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
Chris Lattnerebb61642011-06-16 22:56:45 +0000412 "InstallationFolder",
413 windowsSDKInstallDir,
414 sizeof(windowsSDKInstallDir) - 1);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000415 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000416 if (hasSDKDir && windowsSDKInstallDir[0]) {
417 path = windowsSDKInstallDir;
418 return(true);
419 }
420 return(false);
421}
422
mike-m79bc57c2010-05-16 19:03:52 +0000423void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
424 const HeaderSearchOptions &HSOpts) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000425 llvm::Triple::OSType os = triple.getOS();
426
427 switch (os) {
Roman Divacky63076602011-03-01 18:08:03 +0000428 case llvm::Triple::FreeBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000429 case llvm::Triple::NetBSD:
430 break;
431 default:
432 // FIXME: temporary hack: hard-coded paths.
433 AddPath("/usr/local/include", System, true, false, false);
434 break;
435 }
mike-m79bc57c2010-05-16 19:03:52 +0000436
437 // Builtin includes use #include_next directives and should be positioned
438 // just prior C include dirs.
439 if (HSOpts.UseBuiltinIncludes) {
440 // Ignore the sys root, we *always* look for clang headers relative to
441 // supplied path.
442 llvm::sys::Path P(HSOpts.ResourceDir);
443 P.appendComponent("include");
444 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
445 }
446
447 // Add dirs specified via 'configure --with-c-include-dirs'.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000448 StringRef CIncludeDirs(C_INCLUDE_DIRS);
Daniel Dunbarc7064682009-11-12 07:28:29 +0000449 if (CIncludeDirs != "") {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000450 SmallVector<StringRef, 5> dirs;
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000451 CIncludeDirs.split(dirs, ":");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000452 for (SmallVectorImpl<StringRef>::iterator i = dirs.begin();
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000453 i != dirs.end();
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000454 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000455 AddPath(*i, System, false, false, false);
456 return;
457 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000458
Mike Stump43d81762009-10-08 23:29:47 +0000459 switch (os) {
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000460 case llvm::Triple::Win32: {
461 std::string VSDir;
462 std::string WindowsSDKDir;
463 if (getVisualStudioDir(VSDir)) {
464 AddPath(VSDir + "\\VC\\include", System, false, false, false);
465 if (getWindowsSDKDir(WindowsSDKDir))
466 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
467 else
468 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
469 System, false, false, false);
470 } else {
471 // Default install paths.
472 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
473 System, false, false, false);
474 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
475 System, false, false, false);
476 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000477 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000478 System, false, false, false);
479 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
480 System, false, false, false);
481 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000482 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000483 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000484 }
485 break;
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000486 }
Chris Lattner86ed3a32010-04-11 19:29:39 +0000487 case llvm::Triple::Haiku:
488 AddPath("/boot/common/include", System, true, false, false);
489 AddPath("/boot/develop/headers/os", System, true, false, false);
490 AddPath("/boot/develop/headers/os/app", System, true, false, false);
491 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
492 AddPath("/boot/develop/headers/os/device", System, true, false, false);
493 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000494 AddPath("/boot/develop/headers/os/game", System, true, false, false);
Chris Lattner86ed3a32010-04-11 19:29:39 +0000495 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
496 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
497 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
498 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
499 AddPath("/boot/develop/headers/os/media", System, true, false, false);
500 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
501 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
502 AddPath("/boot/develop/headers/os/net", System, true, false, false);
503 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
504 AddPath("/boot/develop/headers/os/support", System, true, false, false);
505 AddPath("/boot/develop/headers/os/translation",
506 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000507 AddPath("/boot/develop/headers/os/add-ons/graphics",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000508 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000509 AddPath("/boot/develop/headers/os/add-ons/input_server",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000510 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000511 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000512 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000513 AddPath("/boot/develop/headers/os/add-ons/tracker",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000514 System, true, false, false);
515 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
516 System, true, false, false);
517 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
518 System, true, false, false);
519 AddPath("/boot/develop/headers/os/be_apps/Tracker",
520 System, true, false, false);
521 AddPath("/boot/develop/headers/cpp", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000522 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000523 System, true, false, false);
524 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
525 AddPath("/boot/develop/headers/bsd", System, true, false, false);
526 AddPath("/boot/develop/headers/glibc", System, true, false, false);
527 AddPath("/boot/develop/headers/posix", System, true, false, false);
528 AddPath("/boot/develop/headers", System, true, false, false);
Eli Friedmana7e68452010-08-22 01:00:03 +0000529 break;
Douglas Gregordca52262011-07-01 22:41:14 +0000530 case llvm::Triple::RTEMS:
531 break;
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000532 case llvm::Triple::Cygwin:
533 AddPath("/usr/include/w32api", System, true, false, false);
534 break;
Douglas Gregord944a9b2011-06-27 15:47:15 +0000535 case llvm::Triple::MinGW32: {
536 // mingw-w64 crt include paths
537 llvm::sys::Path P(HSOpts.ResourceDir);
538 P.appendComponent("../../../i686-w64-mingw32/include"); // <sysroot>/i686-w64-mingw32/include
539 AddPath(P.str(), System, true, false, false);
540 P = llvm::sys::Path(HSOpts.ResourceDir);
541 P.appendComponent("../../../x86_64-w64-mingw32/include"); // <sysroot>/x86_64-w64-mingw32/include
542 AddPath(P.str(), System, true, false, false);
543 // mingw.org crt include paths
544 P = llvm::sys::Path(HSOpts.ResourceDir);
545 P.appendComponent("../../../include"); // <sysroot>/include
546 AddPath(P.str(), System, true, false, false);
547 AddPath("/mingw/include", System, true, false, false);
548 AddPath("c:/mingw/include", System, true, false, false);
549 }
Mike Stump43d81762009-10-08 23:29:47 +0000550 break;
Douglas Gregord944a9b2011-06-27 15:47:15 +0000551
Eric Christopherb3169da2011-06-03 13:06:30 +0000552 case llvm::Triple::Linux:
553 // Generic Debian multiarch support:
554 if (triple.getArch() == llvm::Triple::x86_64) {
555 AddPath("/usr/include/x86_64-linux-gnu", System, false, false, false);
556 AddPath("/usr/include/i686-linux-gnu/64", System, false, false, false);
557 AddPath("/usr/include/i486-linux-gnu/64", System, false, false, false);
Eric Christopher55ab5b02011-06-03 13:24:15 +0000558 } else if (triple.getArch() == llvm::Triple::x86) {
Eric Christopherb3169da2011-06-03 13:06:30 +0000559 AddPath("/usr/include/x86_64-linux-gnu/32", System, false, false, false);
560 AddPath("/usr/include/i686-linux-gnu", System, false, false, false);
561 AddPath("/usr/include/i486-linux-gnu", System, false, false, false);
Eric Christopher55ab5b02011-06-03 13:24:15 +0000562 } else if (triple.getArch() == llvm::Triple::arm) {
563 AddPath("/usr/include/arm-linux-gnueabi", System, false, false, false);
Eric Christopherb3169da2011-06-03 13:06:30 +0000564 }
Mike Stump43d81762009-10-08 23:29:47 +0000565 default:
Mike Stump43d81762009-10-08 23:29:47 +0000566 break;
567 }
John Thompsond3f88342009-10-13 18:51:32 +0000568
Douglas Gregordca52262011-07-01 22:41:14 +0000569 if ( os != llvm::Triple::RTEMS )
570 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000571}
572
Chris Lattner0e3cc052010-05-05 05:28:39 +0000573void InitHeaderSearch::
Douglas Gregord944a9b2011-06-27 15:47:15 +0000574AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000575 llvm::Triple::OSType os = triple.getOS();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000576 StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000577 if (CxxIncludeRoot != "") {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000578 StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000579 if (CxxIncludeArch == "")
580 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
Chris Lattner0e3cc052010-05-05 05:28:39 +0000581 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
582 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000583 else
584 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
Chris Lattner0e3cc052010-05-05 05:28:39 +0000585 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
586 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000587 return;
588 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000589 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000590
591 if (triple.isOSDarwin()) {
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000592 switch (triple.getArch()) {
593 default: break;
594
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000595 case llvm::Triple::ppc:
Douglas Gregor582c3012010-05-29 01:15:12 +0000596 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000597 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000598 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor616d4362010-05-29 01:21:11 +0000599 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000600 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000601 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor582c3012010-05-29 01:15:12 +0000602 triple);
603 break;
604
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000605 case llvm::Triple::x86:
606 case llvm::Triple::x86_64:
607 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
608 "i686-apple-darwin10", "", "x86_64", triple);
609 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
610 "i686-apple-darwin8", "", "", triple);
611 break;
612
613 case llvm::Triple::arm:
614 case llvm::Triple::thumb:
615 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
616 "arm-apple-darwin10", "v7", "", triple);
617 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
618 "arm-apple-darwin10", "v6", "", triple);
619 break;
620 }
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000621 return;
622 }
623
624 switch (os) {
625 case llvm::Triple::Cygwin:
626 // Cygwin-1.7
627 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
628 // g++-4 / Cygwin-1.5
629 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
630 // FIXME: Do we support g++-3.4.4?
631 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
632 break;
633 case llvm::Triple::MinGW32:
Douglas Gregord944a9b2011-06-27 15:47:15 +0000634 // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32)
635 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.0");
636 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.1");
637 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.2");
638 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.3");
639 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.0");
640 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.1");
Douglas Gregorf732f2b2011-07-05 14:16:05 +0000641 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.2");
642 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0");
Douglas Gregord944a9b2011-06-27 15:47:15 +0000643 // mingw.org C++ include paths
644 AddMinGWCPlusPlusIncludePaths("/mingw/lib/gcc", "mingw32", "4.5.2"); //MSYS
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000645 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000646 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000647 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000648 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000649 case llvm::Triple::DragonFly:
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000650 AddPath("/usr/include/c++/4.1", CXXSystem, true, false, false);
Chris Lattner7a7ca282010-01-09 05:41:14 +0000651 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000652 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000653 //===------------------------------------------------------------------===//
654 // Debian based distros.
655 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
656 //===------------------------------------------------------------------===//
Eric Christopherb3169da2011-06-03 13:06:30 +0000657
658 // Ubuntu 11.11 "Oneiric Ocelot" -- gcc-4.6.0
659 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
660 "x86_64-linux-gnu", "32", "", triple);
661 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
662 "i686-linux-gnu", "", "64", triple);
663 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
664 "i486-linux-gnu", "", "64", triple);
665 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
666 "arm-linux-gnueabi", "", "", triple);
667
Axel Naumann14a01162011-05-04 09:25:56 +0000668 // Ubuntu 11.04 "Natty Narwhal" -- gcc-4.5.2
669 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
670 "x86_64-linux-gnu", "32", "", triple);
671 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
672 "i686-linux-gnu", "", "64", triple);
673 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
674 "i486-linux-gnu", "", "64", triple);
675 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
676 "arm-linux-gnueabi", "", "", triple);
677
Rafael Espindolac12bbe52011-02-15 21:44:06 +0000678 // Ubuntu 10.10 "Maverick Meerkat" -- gcc-4.4.5
Rafael Espindolaadafdba2011-02-15 21:16:43 +0000679 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
680 "i686-linux-gnu", "", "64", triple);
681 // The rest of 10.10 is the same as previous versions.
682
mike-mac78b7a2010-05-06 14:11:13 +0000683 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
684 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
685 // Debian 6.0 "squeeze" -- gcc-4.4.2
686 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
687 "x86_64-linux-gnu", "32", "", triple);
688 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
689 "i486-linux-gnu", "", "64", triple);
Nick Lewyckydb083552011-02-01 21:32:14 +0000690 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
691 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000692 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
693 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
694 // Debian 5.0 "lenny" -- gcc-4.3.2
695 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
696 "x86_64-linux-gnu", "32", "", triple);
697 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
698 "i486-linux-gnu", "", "64", triple);
Rafael Espindolae7d6c2c2010-06-04 14:28:10 +0000699 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
700 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000701 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
702 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
703 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
704 "x86_64-linux-gnu", "32", "", triple);
705 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
706 "i486-linux-gnu", "", "64", triple);
707 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
708 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
709 "x86_64-linux-gnu", "32", "", triple);
710 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
711 "i486-linux-gnu", "", "64", triple);
712
713 //===------------------------------------------------------------------===//
714 // Redhat based distros.
715 //===------------------------------------------------------------------===//
Justin Holewinskief48e052011-10-04 15:35:52 +0000716 // Fedora 15 (GCC 4.6.1)
717 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
718 "x86_64-redhat-linux", "32", "", triple);
719 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
720 "i686-redhat-linux", "", "", triple);
721 // Fedora 15 (GCC 4.6.0)
Eric Christopher8f1cc072011-04-06 18:22:53 +0000722 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
723 "x86_64-redhat-linux", "32", "", triple);
724 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
725 "i686-redhat-linux", "", "", triple);
Rafael Espindola6638b3a2010-11-02 18:39:34 +0000726 // Fedora 14
727 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
728 "x86_64-redhat-linux", "32", "", triple);
729 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
730 "i686-redhat-linux", "", "", triple);
NAKAMURA Takumic3703982011-06-16 12:43:57 +0000731 // RHEL5(gcc44)
732 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
733 "x86_64-redhat-linux6E", "32", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000734 // Fedora 13
Chris Lattner4336e192010-05-29 01:01:38 +0000735 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
736 "x86_64-redhat-linux", "32", "", triple);
737 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
738 "i686-redhat-linux","", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000739 // Fedora 12
740 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
741 "x86_64-redhat-linux", "32", "", triple);
742 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
743 "i686-redhat-linux","", "", triple);
744 // Fedora 12 (pre-FEB-2010)
745 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
746 "x86_64-redhat-linux", "32", "", triple);
747 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
748 "i686-redhat-linux","", "", triple);
749 // Fedora 11
750 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
751 "x86_64-redhat-linux", "32", "", triple);
752 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
753 "i586-redhat-linux","", "", triple);
754 // Fedora 10
755 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
756 "x86_64-redhat-linux", "32", "", triple);
757 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
758 "i386-redhat-linux","", "", triple);
759 // Fedora 9
760 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
761 "x86_64-redhat-linux", "32", "", triple);
762 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
763 "i386-redhat-linux", "", "", triple);
764 // Fedora 8
765 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
766 "x86_64-redhat-linux", "", "", triple);
767 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
768 "i386-redhat-linux", "", "", triple);
Eric Christopher199e09a2011-05-17 23:06:53 +0000769
770 // RHEL 5
771 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.1",
772 "x86_64-redhat-linux", "32", "", triple);
773 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.1",
774 "i386-redhat-linux", "", "", triple);
775
mike-mac78b7a2010-05-06 14:11:13 +0000776
777 //===------------------------------------------------------------------===//
778
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000779 // Exherbo (2010-01-25)
780 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000781 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000782 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000783 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000784
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000785 // openSUSE 11.1 32 bit
786 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000787 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000788 // openSUSE 11.1 64 bit
789 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000790 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000791 // openSUSE 11.2
792 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000793 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000794 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000795 "x86_64-suse-linux", "", "", triple);
Rafael Espindola9d2c0602010-11-25 18:51:59 +0000796
797 // openSUSE 11.4
798 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
799 "i586-suse-linux", "", "", triple);
800 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
801 "x86_64-suse-linux", "", "", triple);
802
David Chisnall2eb3cce2011-05-19 12:04:49 +0000803 // openSUSE 12.1
804 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
805 "i586-suse-linux", "", "", triple);
806 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
807 "x86_64-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000808 // Arch Linux 2008-06-24
809 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000810 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000811 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000812 "x86_64-unknown-linux-gnu", "", "", triple);
Nico Weber014f9722011-04-25 20:59:30 +0000813
814 // Arch Linux gcc 4.6
Chandler Carruth37187cc2011-07-02 00:51:03 +0000815 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
816 "i686-pc-linux-gnu", "", "", triple);
817 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
818 "x86_64-unknown-linux-gnu", "", "", triple);
Nico Weber014f9722011-04-25 20:59:30 +0000819 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
820 "i686-pc-linux-gnu", "", "", triple);
821 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
822 "x86_64-unknown-linux-gnu", "", "", triple);
823
Eli Friedmanb1f2f472011-08-29 18:56:43 +0000824 // Slackware gcc 4.5.2 (13.37)
825 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.2",
826 "i486-slackware-linux", "", "", triple);
827 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.2",
828 "x86_64-slackware-linux", "", "", triple);
829 // Slackware gcc 4.5.3 (-current)
830 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.3",
831 "i486-slackware-linux", "", "", triple);
832 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.3",
833 "x86_64-slackware-linux", "", "", triple);
834
Douglas Gregor53703832011-03-14 15:33:44 +0000835 // Gentoo x86 gcc 4.5.2
836 AddGnuCPlusPlusIncludePaths(
837 "/usr/lib/gcc/i686-pc-linux-gnu/4.5.2/include/g++-v4",
838 "i686-pc-linux-gnu", "", "", triple);
839 // Gentoo x86 gcc 4.4.5
840 AddGnuCPlusPlusIncludePaths(
841 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.5/include/g++-v4",
842 "i686-pc-linux-gnu", "", "", triple);
843 // Gentoo x86 gcc 4.4.4
844 AddGnuCPlusPlusIncludePaths(
845 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.4/include/g++-v4",
846 "i686-pc-linux-gnu", "", "", triple);
847 // Gentoo x86 2010.0 stable
Nuno Lopes0d155a52010-09-11 17:51:45 +0000848 AddGnuCPlusPlusIncludePaths(
849 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
850 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000851 // Gentoo x86 2009.1 stable
852 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000853 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000854 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000855 // Gentoo x86 2009.0 stable
856 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000857 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000858 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000859 // Gentoo x86 2008.0 stable
860 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000861 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000862 "i686-pc-linux-gnu", "", "", triple);
Douglas Gregor53703832011-03-14 15:33:44 +0000863 // Gentoo x86 llvm-gcc trunk
864 AddGnuCPlusPlusIncludePaths(
865 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
866 "i686-pc-linux-gnu", "", "", triple);
Nick Lewycky66935342010-07-24 21:33:13 +0000867
Douglas Gregor53703832011-03-14 15:33:44 +0000868 // Gentoo amd64 gcc 4.5.2
869 AddGnuCPlusPlusIncludePaths(
870 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.2/include/g++-v4",
871 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000872 // Gentoo amd64 gcc 4.4.5
873 AddGnuCPlusPlusIncludePaths(
874 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4",
875 "x86_64-pc-linux-gnu", "32", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000876 // Gentoo amd64 gcc 4.4.4
877 AddGnuCPlusPlusIncludePaths(
878 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
879 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000880 // Gentoo amd64 gcc 4.4.3
881 AddGnuCPlusPlusIncludePaths(
882 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
883 "x86_64-pc-linux-gnu", "32", "", triple);
Nico Weber236a5372011-08-17 17:55:30 +0000884 // Gentoo amd64 gcc 4.3.4
885 AddGnuCPlusPlusIncludePaths(
886 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/include/g++-v4",
887 "x86_64-pc-linux-gnu", "", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000888 // Gentoo amd64 gcc 4.3.2
889 AddGnuCPlusPlusIncludePaths(
890 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
891 "x86_64-pc-linux-gnu", "", "", triple);
892 // Gentoo amd64 stable
893 AddGnuCPlusPlusIncludePaths(
894 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Douglas Gregor53703832011-03-14 15:33:44 +0000895 "x86_64-pc-linux-gnu", "", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000896
Nick Lewycky66935342010-07-24 21:33:13 +0000897 // Gentoo amd64 llvm-gcc trunk
898 AddGnuCPlusPlusIncludePaths(
899 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
900 "x86_64-pc-linux-gnu", "", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000901
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000902 break;
903 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000904 // FreeBSD 8.0
905 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000906 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000907 break;
Anton Korobeynikovab079412010-08-31 22:39:50 +0000908 case llvm::Triple::NetBSD:
909 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
910 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000911 case llvm::Triple::OpenBSD: {
912 std::string t = triple.getTriple();
913 if (t.substr(0, 6) == "x86_64")
914 t.replace(0, 6, "amd64");
915 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
916 t, "", "", triple);
917 break;
918 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000919 case llvm::Triple::Minix:
920 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
921 "", "", "", triple);
922 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000923 case llvm::Triple::Solaris:
924 // Solaris - Fall though..
925 case llvm::Triple::AuroraUX:
926 // AuroraUX
927 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000928 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000929 break;
930 default:
931 break;
932 }
933}
934
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000935void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000936 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000937 const HeaderSearchOptions &HSOpts) {
Bob Wilson13c4f212011-06-21 21:12:29 +0000938 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes) {
Douglas Gregorbaf41f12011-07-29 20:21:18 +0000939 if (HSOpts.UseLibcxx) {
940 if (triple.isOSDarwin()) {
941 // On Darwin, libc++ may be installed alongside the compiler in
942 // lib/c++/v1.
943 llvm::sys::Path P(HSOpts.ResourceDir);
944 if (!P.isEmpty()) {
945 P.eraseComponent(); // Remove version from foo/lib/clang/version
946 P.eraseComponent(); // Remove clang from foo/lib/clang
947
948 // Get foo/lib/c++/v1
949 P.appendComponent("c++");
950 P.appendComponent("v1");
951 AddPath(P.str(), CXXSystem, true, false, false, true);
952 }
953 }
954
Bob Wilson13c4f212011-06-21 21:12:29 +0000955 AddPath("/usr/include/c++/v1", CXXSystem, true, false, false);
Douglas Gregorbaf41f12011-07-29 20:21:18 +0000956 }
Bob Wilson13c4f212011-06-21 21:12:29 +0000957 else
Douglas Gregord944a9b2011-06-27 15:47:15 +0000958 AddDefaultCPlusPlusIncludePaths(triple, HSOpts);
Bob Wilson13c4f212011-06-21 21:12:29 +0000959 }
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000960
mike-m79bc57c2010-05-16 19:03:52 +0000961 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000962
963 // Add the default framework include paths on Darwin.
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000964 if (triple.isOSDarwin()) {
Daniel Dunbare1665822009-11-07 04:20:39 +0000965 AddPath("/System/Library/Frameworks", System, true, false, true);
966 AddPath("/Library/Frameworks", System, true, false, true);
967 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000968}
969
Nico Weber0fca0222008-08-22 09:25:22 +0000970/// RemoveDuplicates - If there are duplicate directory entries in the specified
Chad Rosiere305e812011-10-10 18:44:24 +0000971/// search list, remove the later (dead) ones. Returns the number of non-system
972/// headers removed, which is used to update NumAngled.
973static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
974 unsigned First, bool Verbose) {
Nico Weber0fca0222008-08-22 09:25:22 +0000975 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
976 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
977 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Chad Rosiere305e812011-10-10 18:44:24 +0000978 unsigned NonSystemRemoved = 0;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000979 for (unsigned i = First; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000980 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Chris Lattner43eee072009-02-08 01:00:10 +0000982 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Chris Lattner43eee072009-02-08 01:00:10 +0000984 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000985 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000986 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000987 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000988 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000989 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000990 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000991 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000992 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000993 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000994 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000995 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000996 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000997 }
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Chris Lattner30f05b52009-02-08 00:55:22 +0000999 // If we have a normal #include dir/framework/headermap that is shadowed
1000 // later in the chain by a system include location, we actually want to
1001 // ignore the user's request and drop the user dir... keeping the system
1002 // dir. This is weird, but required to emulate GCC's search path correctly.
1003 //
1004 // Since dupes of system dirs are rare, just rescan to find the original
1005 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +00001006 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +00001007 // Find the dir that this is the same of.
1008 unsigned FirstDir;
1009 for (FirstDir = 0; ; ++FirstDir) {
1010 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Chris Lattner43eee072009-02-08 01:00:10 +00001012 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
1013
Chris Lattner30f05b52009-02-08 00:55:22 +00001014 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +00001015 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +00001016 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Chris Lattner30f05b52009-02-08 00:55:22 +00001018 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +00001019 if (CurEntry.isNormalDir())
1020 isSame = SearchEntry.getDir() == CurEntry.getDir();
1021 else if (CurEntry.isFramework())
1022 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +00001023 else {
Chris Lattner43eee072009-02-08 01:00:10 +00001024 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
1025 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +00001026 }
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattner30f05b52009-02-08 00:55:22 +00001028 if (isSame)
1029 break;
1030 }
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Chris Lattner30f05b52009-02-08 00:55:22 +00001032 // If the first dir in the search path is a non-system dir, zap it
1033 // instead of the system one.
1034 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
1035 DirToRemove = FirstDir;
1036 }
1037
1038 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001039 llvm::errs() << "ignoring duplicate directory \""
1040 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +00001041 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001042 llvm::errs() << " as it is a non-system directory that duplicates "
1043 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001044 }
Chad Rosiere305e812011-10-10 18:44:24 +00001045 if (DirToRemove != i)
1046 ++NonSystemRemoved;
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Chris Lattner7a739402008-09-26 17:46:45 +00001048 // This is reached if the current entry is a duplicate. Remove the
1049 // DirToRemove (usually the current dir).
1050 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +00001051 --i;
1052 }
Chad Rosiere305e812011-10-10 18:44:24 +00001053 return NonSystemRemoved;
Nico Weber0fca0222008-08-22 09:25:22 +00001054}
1055
1056
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001057void InitHeaderSearch::Realize(const LangOptions &Lang) {
Nico Weber0fca0222008-08-22 09:25:22 +00001058 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
1059 std::vector<DirectoryLookup> SearchList;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001060 SearchList.reserve(IncludePath.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Chris Lattnerebb61642011-06-16 22:56:45 +00001062 // Quoted arguments go first.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001063 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1064 it != ie; ++it) {
1065 if (it->first == Quoted)
1066 SearchList.push_back(it->second);
1067 }
Chris Lattnerebb61642011-06-16 22:56:45 +00001068 // Deduplicate and remember index.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001069 RemoveDuplicates(SearchList, 0, Verbose);
Chris Lattnerebb61642011-06-16 22:56:45 +00001070 unsigned NumQuoted = SearchList.size();
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001072 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1073 it != ie; ++it) {
Douglas Gregor65e02fa2011-07-28 04:45:53 +00001074 if (it->first == Angled || it->first == IndexHeaderMap)
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001075 SearchList.push_back(it->second);
1076 }
Chris Lattnerebb61642011-06-16 22:56:45 +00001077
1078 RemoveDuplicates(SearchList, NumQuoted, Verbose);
1079 unsigned NumAngled = SearchList.size();
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001080
1081 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1082 it != ie; ++it) {
Benjamin Kramer47adebe2011-09-22 21:41:16 +00001083 if (it->first == System ||
1084 (!Lang.ObjC1 && !Lang.CPlusPlus && it->first == CSystem) ||
Benjamin Kramerc535d972011-09-23 02:25:14 +00001085 (/*FIXME !Lang.ObjC1 && */Lang.CPlusPlus && it->first == CXXSystem) ||
Benjamin Kramer47adebe2011-09-22 21:41:16 +00001086 (Lang.ObjC1 && !Lang.CPlusPlus && it->first == ObjCSystem) ||
1087 (Lang.ObjC1 && Lang.CPlusPlus && it->first == ObjCXXSystem))
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001088 SearchList.push_back(it->second);
1089 }
1090
1091 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1092 it != ie; ++it) {
1093 if (it->first == After)
1094 SearchList.push_back(it->second);
1095 }
1096
Chris Lattner1d7f12b2011-06-16 22:58:10 +00001097 // Remove duplicates across both the Angled and System directories. GCC does
1098 // this and failing to remove duplicates across these two groups breaks
1099 // #include_next.
Chad Rosiere305e812011-10-10 18:44:24 +00001100 unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose);
1101 NumAngled -= NonSystemRemoved;
Nico Weber0fca0222008-08-22 09:25:22 +00001102
1103 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
Chris Lattnerebb61642011-06-16 22:56:45 +00001104 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
Nico Weber0fca0222008-08-22 09:25:22 +00001105
1106 // If verbose, print the list of directories that will be searched.
1107 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001108 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001109 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
Chris Lattnerebb61642011-06-16 22:56:45 +00001110 if (i == NumQuoted)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001111 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001112 const char *Name = SearchList[i].getName();
1113 const char *Suffix;
1114 if (SearchList[i].isNormalDir())
1115 Suffix = "";
1116 else if (SearchList[i].isFramework())
1117 Suffix = " (framework directory)";
1118 else {
1119 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
1120 Suffix = " (headermap)";
1121 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001122 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001123 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001124 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001125 }
1126}
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001127
Daniel Dunbar5814e652009-11-11 21:44:21 +00001128void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
1129 const HeaderSearchOptions &HSOpts,
1130 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001131 const llvm::Triple &Triple) {
1132 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
1133
1134 // Add the user defined entries.
1135 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
1136 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +00001137 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
Bob Wilson5dd45f12011-06-21 21:53:08 +00001138 E.IgnoreSysRoot);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001139 }
1140
Daniel Dunbardd35ce92009-11-07 04:58:12 +00001141 if (HSOpts.UseStandardIncludes)
mike-m79bc57c2010-05-16 19:03:52 +00001142 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001143
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001144 Init.Realize(Lang);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001145}