blob: b066e71a9435185a2778c7b6ea3ac5cb232a0659 [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.
Daniel Dunbara268fc02011-10-11 18:20:10 +000095 void AddDefaultIncludePaths(const LangOptions &Lang,
96 const llvm::Triple &triple,
97 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
Daniel Dunbara268fc02011-10-11 18:20:10 +0000427 if (HSOpts.UseStandardSystemIncludes) {
428 switch (os) {
429 case llvm::Triple::FreeBSD:
430 case llvm::Triple::NetBSD:
431 break;
432 default:
433 // FIXME: temporary hack: hard-coded paths.
434 AddPath("/usr/local/include", System, true, false, false);
435 break;
436 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000437 }
mike-m79bc57c2010-05-16 19:03:52 +0000438
439 // Builtin includes use #include_next directives and should be positioned
440 // just prior C include dirs.
441 if (HSOpts.UseBuiltinIncludes) {
442 // Ignore the sys root, we *always* look for clang headers relative to
443 // supplied path.
444 llvm::sys::Path P(HSOpts.ResourceDir);
445 P.appendComponent("include");
446 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
447 }
448
Daniel Dunbara268fc02011-10-11 18:20:10 +0000449 // All remaining additions are for system include directories, early exit if
450 // we aren't using them.
451 if (!HSOpts.UseStandardSystemIncludes)
452 return;
453
mike-m79bc57c2010-05-16 19:03:52 +0000454 // Add dirs specified via 'configure --with-c-include-dirs'.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000455 StringRef CIncludeDirs(C_INCLUDE_DIRS);
Daniel Dunbarc7064682009-11-12 07:28:29 +0000456 if (CIncludeDirs != "") {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000457 SmallVector<StringRef, 5> dirs;
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000458 CIncludeDirs.split(dirs, ":");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000459 for (SmallVectorImpl<StringRef>::iterator i = dirs.begin();
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000460 i != dirs.end();
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000461 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000462 AddPath(*i, System, false, false, false);
463 return;
464 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000465
Mike Stump43d81762009-10-08 23:29:47 +0000466 switch (os) {
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000467 case llvm::Triple::Win32: {
468 std::string VSDir;
469 std::string WindowsSDKDir;
470 if (getVisualStudioDir(VSDir)) {
471 AddPath(VSDir + "\\VC\\include", System, false, false, false);
472 if (getWindowsSDKDir(WindowsSDKDir))
473 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
474 else
475 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
476 System, false, false, false);
477 } else {
478 // Default install paths.
479 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
480 System, false, false, false);
481 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
482 System, false, false, false);
483 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000484 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000485 System, false, false, false);
486 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
487 System, false, false, false);
488 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000489 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000490 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000491 }
492 break;
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000493 }
Chris Lattner86ed3a32010-04-11 19:29:39 +0000494 case llvm::Triple::Haiku:
495 AddPath("/boot/common/include", System, true, false, false);
496 AddPath("/boot/develop/headers/os", System, true, false, false);
497 AddPath("/boot/develop/headers/os/app", System, true, false, false);
498 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
499 AddPath("/boot/develop/headers/os/device", System, true, false, false);
500 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000501 AddPath("/boot/develop/headers/os/game", System, true, false, false);
Chris Lattner86ed3a32010-04-11 19:29:39 +0000502 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
503 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
504 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
505 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
506 AddPath("/boot/develop/headers/os/media", System, true, false, false);
507 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
508 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
509 AddPath("/boot/develop/headers/os/net", System, true, false, false);
510 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
511 AddPath("/boot/develop/headers/os/support", System, true, false, false);
512 AddPath("/boot/develop/headers/os/translation",
513 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000514 AddPath("/boot/develop/headers/os/add-ons/graphics",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000515 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000516 AddPath("/boot/develop/headers/os/add-ons/input_server",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000517 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000518 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000519 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000520 AddPath("/boot/develop/headers/os/add-ons/tracker",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000521 System, true, false, false);
522 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
523 System, true, false, false);
524 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
525 System, true, false, false);
526 AddPath("/boot/develop/headers/os/be_apps/Tracker",
527 System, true, false, false);
528 AddPath("/boot/develop/headers/cpp", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000529 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000530 System, true, false, false);
531 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
532 AddPath("/boot/develop/headers/bsd", System, true, false, false);
533 AddPath("/boot/develop/headers/glibc", System, true, false, false);
534 AddPath("/boot/develop/headers/posix", System, true, false, false);
535 AddPath("/boot/develop/headers", System, true, false, false);
Eli Friedmana7e68452010-08-22 01:00:03 +0000536 break;
Douglas Gregordca52262011-07-01 22:41:14 +0000537 case llvm::Triple::RTEMS:
538 break;
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000539 case llvm::Triple::Cygwin:
540 AddPath("/usr/include/w32api", System, true, false, false);
541 break;
Douglas Gregord944a9b2011-06-27 15:47:15 +0000542 case llvm::Triple::MinGW32: {
543 // mingw-w64 crt include paths
544 llvm::sys::Path P(HSOpts.ResourceDir);
545 P.appendComponent("../../../i686-w64-mingw32/include"); // <sysroot>/i686-w64-mingw32/include
546 AddPath(P.str(), System, true, false, false);
547 P = llvm::sys::Path(HSOpts.ResourceDir);
548 P.appendComponent("../../../x86_64-w64-mingw32/include"); // <sysroot>/x86_64-w64-mingw32/include
549 AddPath(P.str(), System, true, false, false);
550 // mingw.org crt include paths
551 P = llvm::sys::Path(HSOpts.ResourceDir);
552 P.appendComponent("../../../include"); // <sysroot>/include
553 AddPath(P.str(), System, true, false, false);
554 AddPath("/mingw/include", System, true, false, false);
555 AddPath("c:/mingw/include", System, true, false, false);
556 }
Mike Stump43d81762009-10-08 23:29:47 +0000557 break;
Douglas Gregord944a9b2011-06-27 15:47:15 +0000558
Eric Christopherb3169da2011-06-03 13:06:30 +0000559 case llvm::Triple::Linux:
560 // Generic Debian multiarch support:
561 if (triple.getArch() == llvm::Triple::x86_64) {
562 AddPath("/usr/include/x86_64-linux-gnu", System, false, false, false);
563 AddPath("/usr/include/i686-linux-gnu/64", System, false, false, false);
564 AddPath("/usr/include/i486-linux-gnu/64", System, false, false, false);
Eric Christopher55ab5b02011-06-03 13:24:15 +0000565 } else if (triple.getArch() == llvm::Triple::x86) {
Eric Christopherb3169da2011-06-03 13:06:30 +0000566 AddPath("/usr/include/x86_64-linux-gnu/32", System, false, false, false);
567 AddPath("/usr/include/i686-linux-gnu", System, false, false, false);
568 AddPath("/usr/include/i486-linux-gnu", System, false, false, false);
Eric Christopher55ab5b02011-06-03 13:24:15 +0000569 } else if (triple.getArch() == llvm::Triple::arm) {
570 AddPath("/usr/include/arm-linux-gnueabi", System, false, false, false);
Eric Christopherb3169da2011-06-03 13:06:30 +0000571 }
Mike Stump43d81762009-10-08 23:29:47 +0000572 default:
Mike Stump43d81762009-10-08 23:29:47 +0000573 break;
574 }
John Thompsond3f88342009-10-13 18:51:32 +0000575
Douglas Gregordca52262011-07-01 22:41:14 +0000576 if ( os != llvm::Triple::RTEMS )
577 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000578}
579
Chris Lattner0e3cc052010-05-05 05:28:39 +0000580void InitHeaderSearch::
Douglas Gregord944a9b2011-06-27 15:47:15 +0000581AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000582 llvm::Triple::OSType os = triple.getOS();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000583 StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000584 if (CxxIncludeRoot != "") {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000585 StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000586 if (CxxIncludeArch == "")
587 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
Chris Lattner0e3cc052010-05-05 05:28:39 +0000588 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
589 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000590 else
591 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
Chris Lattner0e3cc052010-05-05 05:28:39 +0000592 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
593 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000594 return;
595 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000596 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000597
598 if (triple.isOSDarwin()) {
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000599 switch (triple.getArch()) {
600 default: break;
601
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000602 case llvm::Triple::ppc:
Douglas Gregor582c3012010-05-29 01:15:12 +0000603 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000604 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000605 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor616d4362010-05-29 01:21:11 +0000606 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000607 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000608 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor582c3012010-05-29 01:15:12 +0000609 triple);
610 break;
611
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000612 case llvm::Triple::x86:
613 case llvm::Triple::x86_64:
614 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
615 "i686-apple-darwin10", "", "x86_64", triple);
616 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
617 "i686-apple-darwin8", "", "", triple);
618 break;
619
620 case llvm::Triple::arm:
621 case llvm::Triple::thumb:
622 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
623 "arm-apple-darwin10", "v7", "", triple);
624 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
625 "arm-apple-darwin10", "v6", "", triple);
626 break;
627 }
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000628 return;
629 }
630
631 switch (os) {
632 case llvm::Triple::Cygwin:
633 // Cygwin-1.7
634 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
635 // g++-4 / Cygwin-1.5
636 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
637 // FIXME: Do we support g++-3.4.4?
638 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
639 break;
640 case llvm::Triple::MinGW32:
Douglas Gregord944a9b2011-06-27 15:47:15 +0000641 // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32)
642 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.0");
643 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.1");
644 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.2");
645 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.3");
646 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.0");
647 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.1");
Douglas Gregorf732f2b2011-07-05 14:16:05 +0000648 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.2");
649 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0");
Douglas Gregord944a9b2011-06-27 15:47:15 +0000650 // mingw.org C++ include paths
651 AddMinGWCPlusPlusIncludePaths("/mingw/lib/gcc", "mingw32", "4.5.2"); //MSYS
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000652 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000653 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000654 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000655 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000656 case llvm::Triple::DragonFly:
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000657 AddPath("/usr/include/c++/4.1", CXXSystem, true, false, false);
Chris Lattner7a7ca282010-01-09 05:41:14 +0000658 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000659 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000660 //===------------------------------------------------------------------===//
661 // Debian based distros.
662 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
663 //===------------------------------------------------------------------===//
Eric Christopherb3169da2011-06-03 13:06:30 +0000664
665 // Ubuntu 11.11 "Oneiric Ocelot" -- gcc-4.6.0
666 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
667 "x86_64-linux-gnu", "32", "", triple);
668 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
669 "i686-linux-gnu", "", "64", triple);
670 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
671 "i486-linux-gnu", "", "64", triple);
672 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
673 "arm-linux-gnueabi", "", "", triple);
674
Axel Naumann14a01162011-05-04 09:25:56 +0000675 // Ubuntu 11.04 "Natty Narwhal" -- gcc-4.5.2
676 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
677 "x86_64-linux-gnu", "32", "", triple);
678 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
679 "i686-linux-gnu", "", "64", triple);
680 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
681 "i486-linux-gnu", "", "64", triple);
682 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
683 "arm-linux-gnueabi", "", "", triple);
684
Rafael Espindolac12bbe52011-02-15 21:44:06 +0000685 // Ubuntu 10.10 "Maverick Meerkat" -- gcc-4.4.5
Rafael Espindolaadafdba2011-02-15 21:16:43 +0000686 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
687 "i686-linux-gnu", "", "64", triple);
688 // The rest of 10.10 is the same as previous versions.
689
mike-mac78b7a2010-05-06 14:11:13 +0000690 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
691 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
692 // Debian 6.0 "squeeze" -- gcc-4.4.2
693 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
694 "x86_64-linux-gnu", "32", "", triple);
695 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
696 "i486-linux-gnu", "", "64", triple);
Nick Lewyckydb083552011-02-01 21:32:14 +0000697 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
698 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000699 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
700 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
701 // Debian 5.0 "lenny" -- gcc-4.3.2
702 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
703 "x86_64-linux-gnu", "32", "", triple);
704 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
705 "i486-linux-gnu", "", "64", triple);
Rafael Espindolae7d6c2c2010-06-04 14:28:10 +0000706 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
707 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000708 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
709 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
710 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
711 "x86_64-linux-gnu", "32", "", triple);
712 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
713 "i486-linux-gnu", "", "64", triple);
714 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
715 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
716 "x86_64-linux-gnu", "32", "", triple);
717 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
718 "i486-linux-gnu", "", "64", triple);
719
720 //===------------------------------------------------------------------===//
721 // Redhat based distros.
722 //===------------------------------------------------------------------===//
Justin Holewinskief48e052011-10-04 15:35:52 +0000723 // Fedora 15 (GCC 4.6.1)
724 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
725 "x86_64-redhat-linux", "32", "", triple);
726 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
727 "i686-redhat-linux", "", "", triple);
728 // Fedora 15 (GCC 4.6.0)
Eric Christopher8f1cc072011-04-06 18:22:53 +0000729 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
730 "x86_64-redhat-linux", "32", "", triple);
731 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
732 "i686-redhat-linux", "", "", triple);
Rafael Espindola6638b3a2010-11-02 18:39:34 +0000733 // Fedora 14
734 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
735 "x86_64-redhat-linux", "32", "", triple);
736 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
737 "i686-redhat-linux", "", "", triple);
NAKAMURA Takumic3703982011-06-16 12:43:57 +0000738 // RHEL5(gcc44)
739 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
740 "x86_64-redhat-linux6E", "32", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000741 // Fedora 13
Chris Lattner4336e192010-05-29 01:01:38 +0000742 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
743 "x86_64-redhat-linux", "32", "", triple);
744 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
745 "i686-redhat-linux","", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000746 // Fedora 12
747 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
748 "x86_64-redhat-linux", "32", "", triple);
749 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
750 "i686-redhat-linux","", "", triple);
751 // Fedora 12 (pre-FEB-2010)
752 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
753 "x86_64-redhat-linux", "32", "", triple);
754 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
755 "i686-redhat-linux","", "", triple);
756 // Fedora 11
757 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
758 "x86_64-redhat-linux", "32", "", triple);
759 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
760 "i586-redhat-linux","", "", triple);
761 // Fedora 10
762 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
763 "x86_64-redhat-linux", "32", "", triple);
764 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
765 "i386-redhat-linux","", "", triple);
766 // Fedora 9
767 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
768 "x86_64-redhat-linux", "32", "", triple);
769 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
770 "i386-redhat-linux", "", "", triple);
771 // Fedora 8
772 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
773 "x86_64-redhat-linux", "", "", triple);
774 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
775 "i386-redhat-linux", "", "", triple);
Eric Christopher199e09a2011-05-17 23:06:53 +0000776
777 // RHEL 5
778 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.1",
779 "x86_64-redhat-linux", "32", "", triple);
780 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.1",
781 "i386-redhat-linux", "", "", triple);
782
mike-mac78b7a2010-05-06 14:11:13 +0000783
784 //===------------------------------------------------------------------===//
785
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000786 // Exherbo (2010-01-25)
787 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000788 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000789 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000790 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000791
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000792 // openSUSE 11.1 32 bit
793 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000794 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000795 // openSUSE 11.1 64 bit
796 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000797 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000798 // openSUSE 11.2
799 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000800 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000801 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000802 "x86_64-suse-linux", "", "", triple);
Rafael Espindola9d2c0602010-11-25 18:51:59 +0000803
804 // openSUSE 11.4
805 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
806 "i586-suse-linux", "", "", triple);
807 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
808 "x86_64-suse-linux", "", "", triple);
809
David Chisnall2eb3cce2011-05-19 12:04:49 +0000810 // openSUSE 12.1
811 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
812 "i586-suse-linux", "", "", triple);
813 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
814 "x86_64-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000815 // Arch Linux 2008-06-24
816 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000817 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000818 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000819 "x86_64-unknown-linux-gnu", "", "", triple);
Nico Weber014f9722011-04-25 20:59:30 +0000820
821 // Arch Linux gcc 4.6
Chandler Carruth37187cc2011-07-02 00:51:03 +0000822 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
823 "i686-pc-linux-gnu", "", "", triple);
824 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
825 "x86_64-unknown-linux-gnu", "", "", triple);
Nico Weber014f9722011-04-25 20:59:30 +0000826 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
827 "i686-pc-linux-gnu", "", "", triple);
828 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
829 "x86_64-unknown-linux-gnu", "", "", triple);
830
Eli Friedmanb1f2f472011-08-29 18:56:43 +0000831 // Slackware gcc 4.5.2 (13.37)
832 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.2",
833 "i486-slackware-linux", "", "", triple);
834 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.2",
835 "x86_64-slackware-linux", "", "", triple);
836 // Slackware gcc 4.5.3 (-current)
837 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.3",
838 "i486-slackware-linux", "", "", triple);
839 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.3",
840 "x86_64-slackware-linux", "", "", triple);
841
Douglas Gregor53703832011-03-14 15:33:44 +0000842 // Gentoo x86 gcc 4.5.2
843 AddGnuCPlusPlusIncludePaths(
844 "/usr/lib/gcc/i686-pc-linux-gnu/4.5.2/include/g++-v4",
845 "i686-pc-linux-gnu", "", "", triple);
846 // Gentoo x86 gcc 4.4.5
847 AddGnuCPlusPlusIncludePaths(
848 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.5/include/g++-v4",
849 "i686-pc-linux-gnu", "", "", triple);
850 // Gentoo x86 gcc 4.4.4
851 AddGnuCPlusPlusIncludePaths(
852 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.4/include/g++-v4",
853 "i686-pc-linux-gnu", "", "", triple);
854 // Gentoo x86 2010.0 stable
Nuno Lopes0d155a52010-09-11 17:51:45 +0000855 AddGnuCPlusPlusIncludePaths(
856 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
857 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000858 // Gentoo x86 2009.1 stable
859 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000860 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000861 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000862 // Gentoo x86 2009.0 stable
863 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000864 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000865 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000866 // Gentoo x86 2008.0 stable
867 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000868 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000869 "i686-pc-linux-gnu", "", "", triple);
Douglas Gregor53703832011-03-14 15:33:44 +0000870 // Gentoo x86 llvm-gcc trunk
871 AddGnuCPlusPlusIncludePaths(
872 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
873 "i686-pc-linux-gnu", "", "", triple);
Nick Lewycky66935342010-07-24 21:33:13 +0000874
Douglas Gregor53703832011-03-14 15:33:44 +0000875 // Gentoo amd64 gcc 4.5.2
876 AddGnuCPlusPlusIncludePaths(
877 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.2/include/g++-v4",
878 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000879 // Gentoo amd64 gcc 4.4.5
880 AddGnuCPlusPlusIncludePaths(
881 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4",
882 "x86_64-pc-linux-gnu", "32", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000883 // Gentoo amd64 gcc 4.4.4
884 AddGnuCPlusPlusIncludePaths(
885 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
886 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000887 // Gentoo amd64 gcc 4.4.3
888 AddGnuCPlusPlusIncludePaths(
889 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
890 "x86_64-pc-linux-gnu", "32", "", triple);
Nico Weber236a5372011-08-17 17:55:30 +0000891 // Gentoo amd64 gcc 4.3.4
892 AddGnuCPlusPlusIncludePaths(
893 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/include/g++-v4",
894 "x86_64-pc-linux-gnu", "", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000895 // Gentoo amd64 gcc 4.3.2
896 AddGnuCPlusPlusIncludePaths(
897 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
898 "x86_64-pc-linux-gnu", "", "", triple);
899 // Gentoo amd64 stable
900 AddGnuCPlusPlusIncludePaths(
901 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Douglas Gregor53703832011-03-14 15:33:44 +0000902 "x86_64-pc-linux-gnu", "", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000903
Nick Lewycky66935342010-07-24 21:33:13 +0000904 // Gentoo amd64 llvm-gcc trunk
905 AddGnuCPlusPlusIncludePaths(
906 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
907 "x86_64-pc-linux-gnu", "", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000908
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000909 break;
910 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000911 // FreeBSD 8.0
912 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000913 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000914 break;
Anton Korobeynikovab079412010-08-31 22:39:50 +0000915 case llvm::Triple::NetBSD:
916 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
917 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000918 case llvm::Triple::OpenBSD: {
919 std::string t = triple.getTriple();
920 if (t.substr(0, 6) == "x86_64")
921 t.replace(0, 6, "amd64");
922 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
923 t, "", "", triple);
924 break;
925 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000926 case llvm::Triple::Minix:
927 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
928 "", "", "", triple);
929 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000930 case llvm::Triple::Solaris:
931 // Solaris - Fall though..
932 case llvm::Triple::AuroraUX:
933 // AuroraUX
934 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000935 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000936 break;
937 default:
938 break;
939 }
940}
941
Daniel Dunbara268fc02011-10-11 18:20:10 +0000942void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang,
943 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000944 const HeaderSearchOptions &HSOpts) {
Daniel Dunbara268fc02011-10-11 18:20:10 +0000945 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes &&
946 HSOpts.UseStandardSystemIncludes) {
Douglas Gregorbaf41f12011-07-29 20:21:18 +0000947 if (HSOpts.UseLibcxx) {
948 if (triple.isOSDarwin()) {
949 // On Darwin, libc++ may be installed alongside the compiler in
950 // lib/c++/v1.
951 llvm::sys::Path P(HSOpts.ResourceDir);
952 if (!P.isEmpty()) {
953 P.eraseComponent(); // Remove version from foo/lib/clang/version
954 P.eraseComponent(); // Remove clang from foo/lib/clang
955
956 // Get foo/lib/c++/v1
957 P.appendComponent("c++");
958 P.appendComponent("v1");
959 AddPath(P.str(), CXXSystem, true, false, false, true);
960 }
961 }
962
Bob Wilson13c4f212011-06-21 21:12:29 +0000963 AddPath("/usr/include/c++/v1", CXXSystem, true, false, false);
Daniel Dunbara268fc02011-10-11 18:20:10 +0000964 } else {
Douglas Gregord944a9b2011-06-27 15:47:15 +0000965 AddDefaultCPlusPlusIncludePaths(triple, HSOpts);
Daniel Dunbara268fc02011-10-11 18:20:10 +0000966 }
Bob Wilson13c4f212011-06-21 21:12:29 +0000967 }
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000968
mike-m79bc57c2010-05-16 19:03:52 +0000969 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000970
971 // Add the default framework include paths on Darwin.
Daniel Dunbara268fc02011-10-11 18:20:10 +0000972 if (HSOpts.UseStandardSystemIncludes) {
973 if (triple.isOSDarwin()) {
974 AddPath("/System/Library/Frameworks", System, true, false, true);
975 AddPath("/Library/Frameworks", System, true, false, true);
976 }
Daniel Dunbare1665822009-11-07 04:20:39 +0000977 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000978}
979
Nico Weber0fca0222008-08-22 09:25:22 +0000980/// RemoveDuplicates - If there are duplicate directory entries in the specified
Chad Rosiere305e812011-10-10 18:44:24 +0000981/// search list, remove the later (dead) ones. Returns the number of non-system
982/// headers removed, which is used to update NumAngled.
983static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
984 unsigned First, bool Verbose) {
Nico Weber0fca0222008-08-22 09:25:22 +0000985 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
986 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
987 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Chad Rosiere305e812011-10-10 18:44:24 +0000988 unsigned NonSystemRemoved = 0;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000989 for (unsigned i = First; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000990 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Chris Lattner43eee072009-02-08 01:00:10 +0000992 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Chris Lattner43eee072009-02-08 01:00:10 +0000994 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000995 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000996 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000997 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000998 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000999 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +00001000 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +00001001 continue;
Nico Weber0fca0222008-08-22 09:25:22 +00001002 } else {
Chris Lattner43eee072009-02-08 01:00:10 +00001003 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +00001004 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +00001005 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +00001006 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +00001007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Chris Lattner30f05b52009-02-08 00:55:22 +00001009 // If we have a normal #include dir/framework/headermap that is shadowed
1010 // later in the chain by a system include location, we actually want to
1011 // ignore the user's request and drop the user dir... keeping the system
1012 // dir. This is weird, but required to emulate GCC's search path correctly.
1013 //
1014 // Since dupes of system dirs are rare, just rescan to find the original
1015 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +00001016 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +00001017 // Find the dir that this is the same of.
1018 unsigned FirstDir;
1019 for (FirstDir = 0; ; ++FirstDir) {
1020 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Chris Lattner43eee072009-02-08 01:00:10 +00001022 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
1023
Chris Lattner30f05b52009-02-08 00:55:22 +00001024 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +00001025 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +00001026 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattner30f05b52009-02-08 00:55:22 +00001028 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +00001029 if (CurEntry.isNormalDir())
1030 isSame = SearchEntry.getDir() == CurEntry.getDir();
1031 else if (CurEntry.isFramework())
1032 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +00001033 else {
Chris Lattner43eee072009-02-08 01:00:10 +00001034 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
1035 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +00001036 }
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Chris Lattner30f05b52009-02-08 00:55:22 +00001038 if (isSame)
1039 break;
1040 }
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Chris Lattner30f05b52009-02-08 00:55:22 +00001042 // If the first dir in the search path is a non-system dir, zap it
1043 // instead of the system one.
1044 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
1045 DirToRemove = FirstDir;
1046 }
1047
1048 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001049 llvm::errs() << "ignoring duplicate directory \""
1050 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +00001051 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001052 llvm::errs() << " as it is a non-system directory that duplicates "
1053 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001054 }
Chad Rosiere305e812011-10-10 18:44:24 +00001055 if (DirToRemove != i)
1056 ++NonSystemRemoved;
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Chris Lattner7a739402008-09-26 17:46:45 +00001058 // This is reached if the current entry is a duplicate. Remove the
1059 // DirToRemove (usually the current dir).
1060 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +00001061 --i;
1062 }
Chad Rosiere305e812011-10-10 18:44:24 +00001063 return NonSystemRemoved;
Nico Weber0fca0222008-08-22 09:25:22 +00001064}
1065
1066
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001067void InitHeaderSearch::Realize(const LangOptions &Lang) {
Nico Weber0fca0222008-08-22 09:25:22 +00001068 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
1069 std::vector<DirectoryLookup> SearchList;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001070 SearchList.reserve(IncludePath.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Chris Lattnerebb61642011-06-16 22:56:45 +00001072 // Quoted arguments go first.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001073 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1074 it != ie; ++it) {
1075 if (it->first == Quoted)
1076 SearchList.push_back(it->second);
1077 }
Chris Lattnerebb61642011-06-16 22:56:45 +00001078 // Deduplicate and remember index.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001079 RemoveDuplicates(SearchList, 0, Verbose);
Chris Lattnerebb61642011-06-16 22:56:45 +00001080 unsigned NumQuoted = SearchList.size();
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001082 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1083 it != ie; ++it) {
Douglas Gregor65e02fa2011-07-28 04:45:53 +00001084 if (it->first == Angled || it->first == IndexHeaderMap)
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001085 SearchList.push_back(it->second);
1086 }
Chris Lattnerebb61642011-06-16 22:56:45 +00001087
1088 RemoveDuplicates(SearchList, NumQuoted, Verbose);
1089 unsigned NumAngled = SearchList.size();
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001090
1091 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1092 it != ie; ++it) {
Benjamin Kramer47adebe2011-09-22 21:41:16 +00001093 if (it->first == System ||
1094 (!Lang.ObjC1 && !Lang.CPlusPlus && it->first == CSystem) ||
Benjamin Kramerc535d972011-09-23 02:25:14 +00001095 (/*FIXME !Lang.ObjC1 && */Lang.CPlusPlus && it->first == CXXSystem) ||
Benjamin Kramer47adebe2011-09-22 21:41:16 +00001096 (Lang.ObjC1 && !Lang.CPlusPlus && it->first == ObjCSystem) ||
1097 (Lang.ObjC1 && Lang.CPlusPlus && it->first == ObjCXXSystem))
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001098 SearchList.push_back(it->second);
1099 }
1100
1101 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1102 it != ie; ++it) {
1103 if (it->first == After)
1104 SearchList.push_back(it->second);
1105 }
1106
Chris Lattner1d7f12b2011-06-16 22:58:10 +00001107 // Remove duplicates across both the Angled and System directories. GCC does
1108 // this and failing to remove duplicates across these two groups breaks
1109 // #include_next.
Chad Rosiere305e812011-10-10 18:44:24 +00001110 unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose);
1111 NumAngled -= NonSystemRemoved;
Nico Weber0fca0222008-08-22 09:25:22 +00001112
1113 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
Chris Lattnerebb61642011-06-16 22:56:45 +00001114 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
Nico Weber0fca0222008-08-22 09:25:22 +00001115
1116 // If verbose, print the list of directories that will be searched.
1117 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001118 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001119 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
Chris Lattnerebb61642011-06-16 22:56:45 +00001120 if (i == NumQuoted)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001121 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001122 const char *Name = SearchList[i].getName();
1123 const char *Suffix;
1124 if (SearchList[i].isNormalDir())
1125 Suffix = "";
1126 else if (SearchList[i].isFramework())
1127 Suffix = " (framework directory)";
1128 else {
1129 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
1130 Suffix = " (headermap)";
1131 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001132 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001133 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001134 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001135 }
1136}
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001137
Daniel Dunbar5814e652009-11-11 21:44:21 +00001138void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
1139 const HeaderSearchOptions &HSOpts,
1140 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001141 const llvm::Triple &Triple) {
1142 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
1143
1144 // Add the user defined entries.
1145 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
1146 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +00001147 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
Bob Wilson5dd45f12011-06-21 21:53:08 +00001148 E.IgnoreSysRoot);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001149 }
1150
Daniel Dunbara268fc02011-10-11 18:20:10 +00001151 Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001152
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001153 Init.Realize(Lang);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001154}