blob: c19e80a5fdda98cbc0a68937ec463e25779638b4 [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
Chandler Carruthc09265a2010-11-15 07:15:26 +000055 InitHeaderSearch(HeaderSearch &HS, bool verbose, llvm::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.
Benjamin Kramere89ba592009-12-08 12:38:20 +000061 void AddPath(const llvm::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++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000067 void AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
68 llvm::StringRef ArchDir,
69 llvm::StringRef Dir32,
70 llvm::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++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000075 void AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
76 llvm::StringRef Arch,
77 llvm::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.
Douglas Gregord944a9b2011-06-27 15:47:15 +000081 void AddMinGW64CXXPaths(llvm::StringRef Base,
82 llvm::StringRef Version);
NAKAMURA Takumi9db48462011-03-15 02:32:36 +000083
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000084 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
85 /// separator. The processing follows that of the CPATH variable for gcc.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +000086 void AddDelimitedPaths(llvm::StringRef String);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000087
88 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-m79bc57c2010-05-16 19:03:52 +000089 void AddDefaultCIncludePaths(const llvm::Triple &triple,
90 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000091
92 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
93 // compiling c++.
Douglas Gregord944a9b2011-06-27 15:47:15 +000094 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple,
95 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000096
97 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
98 /// that e.g. stdio.h is found.
99 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000100 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000101 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000102
103 /// Realize - Merges all search path lists into one list and send it to
104 /// HeaderSearch.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000105 void Realize(const LangOptions &Lang);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000106};
107
Chris Lattnerebb61642011-06-16 22:56:45 +0000108} // end anonymous namespace.
Nico Weber0fca0222008-08-22 09:25:22 +0000109
Benjamin Kramere89ba592009-12-08 12:38:20 +0000110void InitHeaderSearch::AddPath(const llvm::Twine &Path,
Benjamin Kramer458fb102009-09-05 09:49:39 +0000111 IncludeDirGroup Group, bool isCXXAware,
112 bool isUserSupplied, bool isFramework,
113 bool IgnoreSysRoot) {
Benjamin Kramere89ba592009-12-08 12:38:20 +0000114 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
Nico Weber0fca0222008-08-22 09:25:22 +0000115 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Nico Weber0fca0222008-08-22 09:25:22 +0000117 // Compute the actual path, taking into consideration -isysroot.
Chandler Carruth5853b0f2010-11-15 00:05:18 +0000118 llvm::SmallString<256> MappedPathStorage;
Chandler Carruthf3721452010-11-15 00:48:13 +0000119 llvm::StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Nico Weber0fca0222008-08-22 09:25:22 +0000121 // Handle isysroot.
Rafael Espindola9a7e09d2011-03-02 21:30:07 +0000122 if ((Group == System || Group == CXXSystem) && !IgnoreSysRoot &&
NAKAMURA Takumi0f0cdab2011-05-02 04:50:10 +0000123#if defined(_WIN32)
124 !MappedPathStr.empty() &&
125 llvm::sys::path::is_separator(MappedPathStr[0]) &&
126#else
Michael J. Spencer256053b2010-12-17 21:22:22 +0000127 llvm::sys::path::is_absolute(MappedPathStr) &&
NAKAMURA Takumi0f0cdab2011-05-02 04:50:10 +0000128#endif
Michael J. Spenceraf6530c2010-12-25 20:09:27 +0000129 IsNotEmptyOrRoot) {
Chandler Carruth5619ae52010-11-15 09:28:23 +0000130 MappedPathStorage.clear();
Chandler Carruthc09265a2010-11-15 07:15:26 +0000131 MappedPathStr =
Michael J. Spenceraf6530c2010-12-25 20:09:27 +0000132 (IncludeSysroot + Path).toStringRef(MappedPathStorage);
Nico Weber0fca0222008-08-22 09:25:22 +0000133 }
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Nico Weber0fca0222008-08-22 09:25:22 +0000135 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000136 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000137 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000138 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000139 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000140 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000141 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000142 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
144
Nico Weber0fca0222008-08-22 09:25:22 +0000145 // If the directory exists, add it.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000146 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000147 IncludePath.push_back(std::make_pair(Group, DirectoryLookup(DE, Type,
148 isUserSupplied, isFramework)));
Nico Weber0fca0222008-08-22 09:25:22 +0000149 return;
150 }
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Nico Weber0fca0222008-08-22 09:25:22 +0000152 // Check to see if this is an apple-style headermap (which are not allowed to
153 // be frameworks).
154 if (!isFramework) {
Chris Lattner39b49bc2010-11-23 08:35:12 +0000155 if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000156 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
157 // It is a headermap, add it to the search path.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000158 IncludePath.push_back(std::make_pair(Group, DirectoryLookup(HM, Type,
159 isUserSupplied)));
Nico Weber0fca0222008-08-22 09:25:22 +0000160 return;
161 }
162 }
163 }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Nico Weber0fca0222008-08-22 09:25:22 +0000165 if (Verbose)
Chandler Carruthf3721452010-11-15 00:48:13 +0000166 llvm::errs() << "ignoring nonexistent directory \""
167 << MappedPathStr << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000168}
169
170
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000171void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) {
172 if (at.empty()) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000173 return;
174
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000175 llvm::StringRef::size_type delim;
176 while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) {
177 if (delim == 0)
Nico Weber0fca0222008-08-22 09:25:22 +0000178 AddPath(".", Angled, false, true, false);
179 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000180 AddPath(at.substr(0, delim), Angled, false, true, false);
181 at = at.substr(delim + 1);
Nico Weber0fca0222008-08-22 09:25:22 +0000182 }
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000183
184 if (at.empty())
Nico Weber0fca0222008-08-22 09:25:22 +0000185 AddPath(".", Angled, false, true, false);
186 else
187 AddPath(at, Angled, false, true, false);
188}
189
Benjamin Kramere89ba592009-12-08 12:38:20 +0000190void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
191 llvm::StringRef ArchDir,
192 llvm::StringRef Dir32,
193 llvm::StringRef Dir64,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000194 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000195 // Add the base dir
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000196 AddPath(Base, CXXSystem, true, false, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000197
198 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000199 llvm::Triple::ArchType arch = triple.getArch();
200 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000201 if (is64bit)
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000202 AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000203 else
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000204 AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, true, false, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000205
206 // Add the backward dir
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000207 AddPath(Base + "/backward", CXXSystem, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000208}
Nico Weber0fca0222008-08-22 09:25:22 +0000209
Benjamin Kramere89ba592009-12-08 12:38:20 +0000210void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
211 llvm::StringRef Arch,
212 llvm::StringRef Version) {
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000213 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000214 CXXSystem, true, false, false);
Chris Lattner8e9006b2010-09-03 16:45:53 +0000215 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000216 CXXSystem, true, false, false);
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000217 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000218 CXXSystem, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000219}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000220
Douglas Gregord944a9b2011-06-27 15:47:15 +0000221void InitHeaderSearch::AddMinGW64CXXPaths(llvm::StringRef Base,
222 llvm::StringRef Version) {
223 // Assumes Base is HeaderSearchOpts' ResourceDir
224 AddPath(Base + "/../../../include/c++/" + Version,
NAKAMURA Takumi9db48462011-03-15 02:32:36 +0000225 CXXSystem, true, false, false);
Douglas Gregord944a9b2011-06-27 15:47:15 +0000226 AddPath(Base + "/../../../include/c++/" + Version + "/x86_64-w64-mingw32",
NAKAMURA Takumi9db48462011-03-15 02:32:36 +0000227 CXXSystem, true, false, false);
Douglas Gregord944a9b2011-06-27 15:47:15 +0000228 AddPath(Base + "/../../../include/c++/" + Version + "/i686-w64-mingw32",
229 CXXSystem, true, false, false);
230 AddPath(Base + "/../../../include/c++/" + Version + "/backward",
NAKAMURA Takumi9db48462011-03-15 02:32:36 +0000231 CXXSystem, true, false, false);
232}
233
Mike Stump620d57a2009-10-12 20:50:45 +0000234 // FIXME: This probably should goto to some platform utils place.
235#ifdef _MSC_VER
John Thompson75ee3bd2009-11-21 00:15:52 +0000236
Mike Stump620d57a2009-10-12 20:50:45 +0000237 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000238 // This also supports a means to look for high-versioned keys by use
239 // of a $VERSION placeholder in the key path.
240 // $VERSION in the key path is a placeholder for the version number,
241 // causing the highest value path to be searched for and used.
242 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
243 // There can be additional characters in the component. Only the numberic
244 // characters are compared.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000245static bool getSystemRegistryString(const char *keyPath, const char *valueName,
246 char *value, size_t maxLength) {
Mike Stump43d81762009-10-08 23:29:47 +0000247 HKEY hRootKey = NULL;
248 HKEY hKey = NULL;
249 const char* subKey = NULL;
250 DWORD valueType;
251 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000252 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000253 bool returnValue = false;
Chris Lattnerebb61642011-06-16 22:56:45 +0000254
Mike Stump43d81762009-10-08 23:29:47 +0000255 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
256 hRootKey = HKEY_CLASSES_ROOT;
257 subKey = keyPath + 18;
Chris Lattnerebb61642011-06-16 22:56:45 +0000258 } else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
Mike Stump43d81762009-10-08 23:29:47 +0000259 hRootKey = HKEY_USERS;
260 subKey = keyPath + 11;
Chris Lattnerebb61642011-06-16 22:56:45 +0000261 } else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
Mike Stump43d81762009-10-08 23:29:47 +0000262 hRootKey = HKEY_LOCAL_MACHINE;
263 subKey = keyPath + 19;
Chris Lattnerebb61642011-06-16 22:56:45 +0000264 } else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
Mike Stump43d81762009-10-08 23:29:47 +0000265 hRootKey = HKEY_CURRENT_USER;
266 subKey = keyPath + 18;
267 }
268 else
Chris Lattnerebb61642011-06-16 22:56:45 +0000269 return false;
270
John Thompson75ee3bd2009-11-21 00:15:52 +0000271 const char *placeHolder = strstr(subKey, "$VERSION");
272 char bestName[256];
273 bestName[0] = '\0';
274 // If we have a $VERSION placeholder, do the highest-version search.
275 if (placeHolder) {
276 const char *keyEnd = placeHolder - 1;
277 const char *nextKey = placeHolder;
278 // Find end of previous key.
279 while ((keyEnd > subKey) && (*keyEnd != '\\'))
280 keyEnd--;
281 // Find end of key containing $VERSION.
282 while (*nextKey && (*nextKey != '\\'))
283 nextKey++;
284 size_t partialKeyLength = keyEnd - subKey;
285 char partialKey[256];
286 if (partialKeyLength > sizeof(partialKey))
287 partialKeyLength = sizeof(partialKey);
288 strncpy(partialKey, subKey, partialKeyLength);
289 partialKey[partialKeyLength] = '\0';
290 HKEY hTopKey = NULL;
291 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
292 if (lResult == ERROR_SUCCESS) {
293 char keyName[256];
294 int bestIndex = -1;
295 double bestValue = 0.0;
296 DWORD index, size = sizeof(keyName) - 1;
Nuno Lopes33cc2432009-12-07 17:18:48 +0000297 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
298 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
299 const char *sp = keyName;
300 while (*sp && !isdigit(*sp))
301 sp++;
302 if (!*sp)
303 continue;
304 const char *ep = sp + 1;
305 while (*ep && (isdigit(*ep) || (*ep == '.')))
306 ep++;
307 char numBuf[32];
308 strncpy(numBuf, sp, sizeof(numBuf) - 1);
309 numBuf[sizeof(numBuf) - 1] = '\0';
310 double value = strtod(numBuf, NULL);
311 if (value > bestValue) {
312 bestIndex = (int)index;
313 bestValue = value;
314 strcpy(bestName, keyName);
315 }
John Thompson75ee3bd2009-11-21 00:15:52 +0000316 size = sizeof(keyName) - 1;
317 }
318 // If we found the highest versioned key, open the key and get the value.
319 if (bestIndex != -1) {
320 // Append rest of key.
321 strncat(bestName, nextKey, sizeof(bestName) - 1);
322 bestName[sizeof(bestName) - 1] = '\0';
323 // Open the chosen key path remainder.
324 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
325 if (lResult == ERROR_SUCCESS) {
326 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
327 (LPBYTE)value, &valueSize);
328 if (lResult == ERROR_SUCCESS)
329 returnValue = true;
330 RegCloseKey(hKey);
331 }
332 }
333 RegCloseKey(hTopKey);
334 }
335 }
336 else {
337 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
338 if (lResult == ERROR_SUCCESS) {
339 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
340 (LPBYTE)value, &valueSize);
341 if (lResult == ERROR_SUCCESS)
342 returnValue = true;
343 RegCloseKey(hKey);
344 }
Mike Stump43d81762009-10-08 23:29:47 +0000345 }
Chris Lattnerebb61642011-06-16 22:56:45 +0000346 return returnValue;
Mike Stump43d81762009-10-08 23:29:47 +0000347}
Mike Stump620d57a2009-10-12 20:50:45 +0000348#else // _MSC_VER
349 // Read registry string.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000350static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
Mike Stump620d57a2009-10-12 20:50:45 +0000351 return(false);
352}
353#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000354
Mike Stump43d81762009-10-08 23:29:47 +0000355 // Get Visual Studio installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000356static bool getVisualStudioDir(std::string &path) {
Michael J. Spencerff58e362010-08-21 21:55:07 +0000357 // First check the environment variables that vsvars32.bat sets.
358 const char* vcinstalldir = getenv("VCINSTALLDIR");
Chris Lattnerebb61642011-06-16 22:56:45 +0000359 if (vcinstalldir) {
Michael J. Spencerff58e362010-08-21 21:55:07 +0000360 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
361 if (p)
362 *p = '\0';
363 path = vcinstalldir;
Chris Lattnerebb61642011-06-16 22:56:45 +0000364 return true;
Michael J. Spencerff58e362010-08-21 21:55:07 +0000365 }
366
John Thompson75ee3bd2009-11-21 00:15:52 +0000367 char vsIDEInstallDir[256];
Douglas Gregor80f93d92010-05-18 05:47:04 +0000368 char vsExpressIDEInstallDir[256];
Michael J. Spencerff58e362010-08-21 21:55:07 +0000369 // Then try the windows registry.
John Thompson75ee3bd2009-11-21 00:15:52 +0000370 bool hasVCDir = getSystemRegistryString(
371 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
372 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Douglas Gregor80f93d92010-05-18 05:47:04 +0000373 bool hasVCExpressDir = getSystemRegistryString(
374 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
375 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000376 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000377 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000378 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
379 if (p)
380 *p = '\0';
381 path = vsIDEInstallDir;
Chris Lattnerebb61642011-06-16 22:56:45 +0000382 return true;
Mike Stump620d57a2009-10-12 20:50:45 +0000383 }
Chris Lattnerebb61642011-06-16 22:56:45 +0000384
385 if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
Douglas Gregor80f93d92010-05-18 05:47:04 +0000386 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
387 if (p)
388 *p = '\0';
389 path = vsExpressIDEInstallDir;
Chris Lattnerebb61642011-06-16 22:56:45 +0000390 return true;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000391 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000392
Chris Lattnerebb61642011-06-16 22:56:45 +0000393 // Try the environment.
394 const char *vs100comntools = getenv("VS100COMNTOOLS");
395 const char *vs90comntools = getenv("VS90COMNTOOLS");
396 const char *vs80comntools = getenv("VS80COMNTOOLS");
397 const char *vscomntools = NULL;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000398
Chris Lattnerebb61642011-06-16 22:56:45 +0000399 // Try to find the version that we were compiled with
400 if(false) {}
401 #if (_MSC_VER >= 1600) // VC100
402 else if(vs100comntools) {
403 vscomntools = vs100comntools;
Mike Stump43d81762009-10-08 23:29:47 +0000404 }
Chris Lattnerebb61642011-06-16 22:56:45 +0000405 #elif (_MSC_VER == 1500) // VC80
406 else if(vs90comntools) {
407 vscomntools = vs90comntools;
408 }
409 #elif (_MSC_VER == 1400) // VC80
410 else if(vs80comntools) {
411 vscomntools = vs80comntools;
412 }
413 #endif
414 // Otherwise find any version we can
415 else if (vs100comntools)
416 vscomntools = vs100comntools;
417 else if (vs90comntools)
418 vscomntools = vs90comntools;
419 else if (vs80comntools)
420 vscomntools = vs80comntools;
421
422 if (vscomntools && *vscomntools) {
423 char *p = const_cast<char *>(strstr(vscomntools, "\\Common7\\Tools"));
424 if (p)
425 *p = '\0';
426 path = vscomntools;
427 return true;
428 }
429 return false;
Mike Stump43d81762009-10-08 23:29:47 +0000430}
Mike Stump43d81762009-10-08 23:29:47 +0000431
John Thompson75ee3bd2009-11-21 00:15:52 +0000432 // Get Windows SDK installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000433static bool getWindowsSDKDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000434 char windowsSDKInstallDir[256];
435 // Try the Windows registry.
436 bool hasSDKDir = getSystemRegistryString(
437 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
Chris Lattnerebb61642011-06-16 22:56:45 +0000438 "InstallationFolder",
439 windowsSDKInstallDir,
440 sizeof(windowsSDKInstallDir) - 1);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000441 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000442 if (hasSDKDir && windowsSDKInstallDir[0]) {
443 path = windowsSDKInstallDir;
444 return(true);
445 }
446 return(false);
447}
448
mike-m79bc57c2010-05-16 19:03:52 +0000449void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
450 const HeaderSearchOptions &HSOpts) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000451 llvm::Triple::OSType os = triple.getOS();
452
453 switch (os) {
Roman Divacky63076602011-03-01 18:08:03 +0000454 case llvm::Triple::FreeBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000455 case llvm::Triple::NetBSD:
456 break;
457 default:
458 // FIXME: temporary hack: hard-coded paths.
459 AddPath("/usr/local/include", System, true, false, false);
460 break;
461 }
mike-m79bc57c2010-05-16 19:03:52 +0000462
463 // Builtin includes use #include_next directives and should be positioned
464 // just prior C include dirs.
465 if (HSOpts.UseBuiltinIncludes) {
466 // Ignore the sys root, we *always* look for clang headers relative to
467 // supplied path.
468 llvm::sys::Path P(HSOpts.ResourceDir);
469 P.appendComponent("include");
470 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
471 }
472
473 // Add dirs specified via 'configure --with-c-include-dirs'.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000474 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
475 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000476 llvm::SmallVector<llvm::StringRef, 5> dirs;
477 CIncludeDirs.split(dirs, ":");
478 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
479 i != dirs.end();
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000480 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000481 AddPath(*i, System, false, false, false);
482 return;
483 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000484
Mike Stump43d81762009-10-08 23:29:47 +0000485 switch (os) {
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000486 case llvm::Triple::Win32: {
487 std::string VSDir;
488 std::string WindowsSDKDir;
489 if (getVisualStudioDir(VSDir)) {
490 AddPath(VSDir + "\\VC\\include", System, false, false, false);
491 if (getWindowsSDKDir(WindowsSDKDir))
492 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
493 else
494 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
495 System, false, false, false);
496 } else {
497 // Default install paths.
498 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
499 System, false, false, false);
500 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
501 System, false, false, false);
502 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000503 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000504 System, false, false, false);
505 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
506 System, false, false, false);
507 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000508 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000509 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000510 }
511 break;
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000512 }
Chris Lattner86ed3a32010-04-11 19:29:39 +0000513 case llvm::Triple::Haiku:
514 AddPath("/boot/common/include", System, true, false, false);
515 AddPath("/boot/develop/headers/os", System, true, false, false);
516 AddPath("/boot/develop/headers/os/app", System, true, false, false);
517 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
518 AddPath("/boot/develop/headers/os/device", System, true, false, false);
519 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000520 AddPath("/boot/develop/headers/os/game", System, true, false, false);
Chris Lattner86ed3a32010-04-11 19:29:39 +0000521 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
522 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
523 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
524 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
525 AddPath("/boot/develop/headers/os/media", System, true, false, false);
526 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
527 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
528 AddPath("/boot/develop/headers/os/net", System, true, false, false);
529 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
530 AddPath("/boot/develop/headers/os/support", System, true, false, false);
531 AddPath("/boot/develop/headers/os/translation",
532 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000533 AddPath("/boot/develop/headers/os/add-ons/graphics",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000534 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000535 AddPath("/boot/develop/headers/os/add-ons/input_server",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000536 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000537 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000538 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000539 AddPath("/boot/develop/headers/os/add-ons/tracker",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000540 System, true, false, false);
541 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
542 System, true, false, false);
543 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
544 System, true, false, false);
545 AddPath("/boot/develop/headers/os/be_apps/Tracker",
546 System, true, false, false);
547 AddPath("/boot/develop/headers/cpp", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000548 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000549 System, true, false, false);
550 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
551 AddPath("/boot/develop/headers/bsd", System, true, false, false);
552 AddPath("/boot/develop/headers/glibc", System, true, false, false);
553 AddPath("/boot/develop/headers/posix", System, true, false, false);
554 AddPath("/boot/develop/headers", System, true, false, false);
Eli Friedmana7e68452010-08-22 01:00:03 +0000555 break;
Douglas Gregordca52262011-07-01 22:41:14 +0000556 case llvm::Triple::RTEMS:
557 break;
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000558 case llvm::Triple::Cygwin:
559 AddPath("/usr/include/w32api", System, true, false, false);
560 break;
Douglas Gregord944a9b2011-06-27 15:47:15 +0000561 case llvm::Triple::MinGW32: {
562 // mingw-w64 crt include paths
563 llvm::sys::Path P(HSOpts.ResourceDir);
564 P.appendComponent("../../../i686-w64-mingw32/include"); // <sysroot>/i686-w64-mingw32/include
565 AddPath(P.str(), System, true, false, false);
566 P = llvm::sys::Path(HSOpts.ResourceDir);
567 P.appendComponent("../../../x86_64-w64-mingw32/include"); // <sysroot>/x86_64-w64-mingw32/include
568 AddPath(P.str(), System, true, false, false);
569 // mingw.org crt include paths
570 P = llvm::sys::Path(HSOpts.ResourceDir);
571 P.appendComponent("../../../include"); // <sysroot>/include
572 AddPath(P.str(), System, true, false, false);
573 AddPath("/mingw/include", System, true, false, false);
574 AddPath("c:/mingw/include", System, true, false, false);
575 }
Mike Stump43d81762009-10-08 23:29:47 +0000576 break;
Douglas Gregord944a9b2011-06-27 15:47:15 +0000577
Eric Christopherb3169da2011-06-03 13:06:30 +0000578 case llvm::Triple::Linux:
579 // Generic Debian multiarch support:
580 if (triple.getArch() == llvm::Triple::x86_64) {
581 AddPath("/usr/include/x86_64-linux-gnu", System, false, false, false);
582 AddPath("/usr/include/i686-linux-gnu/64", System, false, false, false);
583 AddPath("/usr/include/i486-linux-gnu/64", System, false, false, false);
Eric Christopher55ab5b02011-06-03 13:24:15 +0000584 } else if (triple.getArch() == llvm::Triple::x86) {
Eric Christopherb3169da2011-06-03 13:06:30 +0000585 AddPath("/usr/include/x86_64-linux-gnu/32", System, false, false, false);
586 AddPath("/usr/include/i686-linux-gnu", System, false, false, false);
587 AddPath("/usr/include/i486-linux-gnu", System, false, false, false);
Eric Christopher55ab5b02011-06-03 13:24:15 +0000588 } else if (triple.getArch() == llvm::Triple::arm) {
589 AddPath("/usr/include/arm-linux-gnueabi", System, false, false, false);
Eric Christopherb3169da2011-06-03 13:06:30 +0000590 }
Mike Stump43d81762009-10-08 23:29:47 +0000591 default:
Mike Stump43d81762009-10-08 23:29:47 +0000592 break;
593 }
John Thompsond3f88342009-10-13 18:51:32 +0000594
Douglas Gregordca52262011-07-01 22:41:14 +0000595 if ( os != llvm::Triple::RTEMS )
596 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000597}
598
Chris Lattner0e3cc052010-05-05 05:28:39 +0000599void InitHeaderSearch::
Douglas Gregord944a9b2011-06-27 15:47:15 +0000600AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000601 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000602 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
603 if (CxxIncludeRoot != "") {
604 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
605 if (CxxIncludeArch == "")
606 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
Chris Lattner0e3cc052010-05-05 05:28:39 +0000607 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
608 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000609 else
610 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
Chris Lattner0e3cc052010-05-05 05:28:39 +0000611 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
612 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000613 return;
614 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000615 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000616
617 if (triple.isOSDarwin()) {
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000618 switch (triple.getArch()) {
619 default: break;
620
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000621 case llvm::Triple::ppc:
Douglas Gregor582c3012010-05-29 01:15:12 +0000622 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000623 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000624 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor616d4362010-05-29 01:21:11 +0000625 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000626 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000627 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor582c3012010-05-29 01:15:12 +0000628 triple);
629 break;
630
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000631 case llvm::Triple::x86:
632 case llvm::Triple::x86_64:
633 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
634 "i686-apple-darwin10", "", "x86_64", triple);
635 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
636 "i686-apple-darwin8", "", "", triple);
637 break;
638
639 case llvm::Triple::arm:
640 case llvm::Triple::thumb:
641 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
642 "arm-apple-darwin10", "v7", "", triple);
643 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
644 "arm-apple-darwin10", "v6", "", triple);
645 break;
646 }
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000647 return;
648 }
649
650 switch (os) {
651 case llvm::Triple::Cygwin:
652 // Cygwin-1.7
653 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
654 // g++-4 / Cygwin-1.5
655 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
656 // FIXME: Do we support g++-3.4.4?
657 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
658 break;
659 case llvm::Triple::MinGW32:
Douglas Gregord944a9b2011-06-27 15:47:15 +0000660 // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32)
661 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.0");
662 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.1");
663 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.2");
664 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.3");
665 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.0");
666 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.1");
Douglas Gregorf732f2b2011-07-05 14:16:05 +0000667 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.2");
668 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0");
Douglas Gregord944a9b2011-06-27 15:47:15 +0000669 // mingw.org C++ include paths
670 AddMinGWCPlusPlusIncludePaths("/mingw/lib/gcc", "mingw32", "4.5.2"); //MSYS
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000671 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000672 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000673 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000674 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000675 case llvm::Triple::DragonFly:
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000676 AddPath("/usr/include/c++/4.1", CXXSystem, true, false, false);
Chris Lattner7a7ca282010-01-09 05:41:14 +0000677 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000678 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000679 //===------------------------------------------------------------------===//
680 // Debian based distros.
681 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
682 //===------------------------------------------------------------------===//
Eric Christopherb3169da2011-06-03 13:06:30 +0000683
684 // Ubuntu 11.11 "Oneiric Ocelot" -- gcc-4.6.0
685 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
686 "x86_64-linux-gnu", "32", "", triple);
687 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
688 "i686-linux-gnu", "", "64", triple);
689 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
690 "i486-linux-gnu", "", "64", triple);
691 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
692 "arm-linux-gnueabi", "", "", triple);
693
Axel Naumann14a01162011-05-04 09:25:56 +0000694 // Ubuntu 11.04 "Natty Narwhal" -- gcc-4.5.2
695 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
696 "x86_64-linux-gnu", "32", "", triple);
697 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
698 "i686-linux-gnu", "", "64", triple);
699 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
700 "i486-linux-gnu", "", "64", triple);
701 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
702 "arm-linux-gnueabi", "", "", triple);
703
Rafael Espindolac12bbe52011-02-15 21:44:06 +0000704 // Ubuntu 10.10 "Maverick Meerkat" -- gcc-4.4.5
Rafael Espindolaadafdba2011-02-15 21:16:43 +0000705 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
706 "i686-linux-gnu", "", "64", triple);
707 // The rest of 10.10 is the same as previous versions.
708
mike-mac78b7a2010-05-06 14:11:13 +0000709 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
710 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
711 // Debian 6.0 "squeeze" -- gcc-4.4.2
712 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
713 "x86_64-linux-gnu", "32", "", triple);
714 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
715 "i486-linux-gnu", "", "64", triple);
Nick Lewyckydb083552011-02-01 21:32:14 +0000716 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
717 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000718 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
719 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
720 // Debian 5.0 "lenny" -- gcc-4.3.2
721 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
722 "x86_64-linux-gnu", "32", "", triple);
723 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
724 "i486-linux-gnu", "", "64", triple);
Rafael Espindolae7d6c2c2010-06-04 14:28:10 +0000725 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
726 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000727 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
728 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
729 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
730 "x86_64-linux-gnu", "32", "", triple);
731 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
732 "i486-linux-gnu", "", "64", triple);
733 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
734 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
735 "x86_64-linux-gnu", "32", "", triple);
736 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
737 "i486-linux-gnu", "", "64", triple);
738
739 //===------------------------------------------------------------------===//
740 // Redhat based distros.
741 //===------------------------------------------------------------------===//
Eric Christopher8f1cc072011-04-06 18:22:53 +0000742 // Fedora 15
743 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
744 "x86_64-redhat-linux", "32", "", triple);
745 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
746 "i686-redhat-linux", "", "", triple);
Rafael Espindola6638b3a2010-11-02 18:39:34 +0000747 // Fedora 14
748 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
749 "x86_64-redhat-linux", "32", "", triple);
750 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
751 "i686-redhat-linux", "", "", triple);
NAKAMURA Takumic3703982011-06-16 12:43:57 +0000752 // RHEL5(gcc44)
753 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
754 "x86_64-redhat-linux6E", "32", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000755 // Fedora 13
Chris Lattner4336e192010-05-29 01:01:38 +0000756 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
757 "x86_64-redhat-linux", "32", "", triple);
758 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
759 "i686-redhat-linux","", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000760 // Fedora 12
761 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
762 "x86_64-redhat-linux", "32", "", triple);
763 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
764 "i686-redhat-linux","", "", triple);
765 // Fedora 12 (pre-FEB-2010)
766 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
767 "x86_64-redhat-linux", "32", "", triple);
768 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
769 "i686-redhat-linux","", "", triple);
770 // Fedora 11
771 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
772 "x86_64-redhat-linux", "32", "", triple);
773 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
774 "i586-redhat-linux","", "", triple);
775 // Fedora 10
776 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
777 "x86_64-redhat-linux", "32", "", triple);
778 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
779 "i386-redhat-linux","", "", triple);
780 // Fedora 9
781 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
782 "x86_64-redhat-linux", "32", "", triple);
783 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
784 "i386-redhat-linux", "", "", triple);
785 // Fedora 8
786 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
787 "x86_64-redhat-linux", "", "", triple);
788 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
789 "i386-redhat-linux", "", "", triple);
Eric Christopher199e09a2011-05-17 23:06:53 +0000790
791 // RHEL 5
792 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.1",
793 "x86_64-redhat-linux", "32", "", triple);
794 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.1",
795 "i386-redhat-linux", "", "", triple);
796
mike-mac78b7a2010-05-06 14:11:13 +0000797
798 //===------------------------------------------------------------------===//
799
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000800 // Exherbo (2010-01-25)
801 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000802 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000803 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000804 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000805
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000806 // openSUSE 11.1 32 bit
807 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000808 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000809 // openSUSE 11.1 64 bit
810 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000811 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000812 // openSUSE 11.2
813 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000814 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000815 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000816 "x86_64-suse-linux", "", "", triple);
Rafael Espindola9d2c0602010-11-25 18:51:59 +0000817
818 // openSUSE 11.4
819 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
820 "i586-suse-linux", "", "", triple);
821 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
822 "x86_64-suse-linux", "", "", triple);
823
David Chisnall2eb3cce2011-05-19 12:04:49 +0000824 // openSUSE 12.1
825 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
826 "i586-suse-linux", "", "", triple);
827 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
828 "x86_64-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000829 // Arch Linux 2008-06-24
830 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000831 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000832 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000833 "x86_64-unknown-linux-gnu", "", "", triple);
Nico Weber014f9722011-04-25 20:59:30 +0000834
835 // Arch Linux gcc 4.6
Chandler Carruth37187cc2011-07-02 00:51:03 +0000836 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
837 "i686-pc-linux-gnu", "", "", triple);
838 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
839 "x86_64-unknown-linux-gnu", "", "", triple);
Nico Weber014f9722011-04-25 20:59:30 +0000840 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
841 "i686-pc-linux-gnu", "", "", triple);
842 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
843 "x86_64-unknown-linux-gnu", "", "", triple);
844
Douglas Gregor53703832011-03-14 15:33:44 +0000845 // Gentoo x86 gcc 4.5.2
846 AddGnuCPlusPlusIncludePaths(
847 "/usr/lib/gcc/i686-pc-linux-gnu/4.5.2/include/g++-v4",
848 "i686-pc-linux-gnu", "", "", triple);
849 // Gentoo x86 gcc 4.4.5
850 AddGnuCPlusPlusIncludePaths(
851 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.5/include/g++-v4",
852 "i686-pc-linux-gnu", "", "", triple);
853 // Gentoo x86 gcc 4.4.4
854 AddGnuCPlusPlusIncludePaths(
855 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.4/include/g++-v4",
856 "i686-pc-linux-gnu", "", "", triple);
857 // Gentoo x86 2010.0 stable
Nuno Lopes0d155a52010-09-11 17:51:45 +0000858 AddGnuCPlusPlusIncludePaths(
859 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
860 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000861 // Gentoo x86 2009.1 stable
862 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000863 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000864 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000865 // Gentoo x86 2009.0 stable
866 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000867 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000868 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000869 // Gentoo x86 2008.0 stable
870 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000871 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000872 "i686-pc-linux-gnu", "", "", triple);
Douglas Gregor53703832011-03-14 15:33:44 +0000873 // Gentoo x86 llvm-gcc trunk
874 AddGnuCPlusPlusIncludePaths(
875 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
876 "i686-pc-linux-gnu", "", "", triple);
Nick Lewycky66935342010-07-24 21:33:13 +0000877
Douglas Gregor53703832011-03-14 15:33:44 +0000878 // Gentoo amd64 gcc 4.5.2
879 AddGnuCPlusPlusIncludePaths(
880 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.2/include/g++-v4",
881 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000882 // Gentoo amd64 gcc 4.4.5
883 AddGnuCPlusPlusIncludePaths(
884 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4",
885 "x86_64-pc-linux-gnu", "32", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000886 // Gentoo amd64 gcc 4.4.4
887 AddGnuCPlusPlusIncludePaths(
888 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
889 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000890 // Gentoo amd64 gcc 4.4.3
891 AddGnuCPlusPlusIncludePaths(
892 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
893 "x86_64-pc-linux-gnu", "32", "", triple);
894 // Gentoo amd64 gcc 4.3.2
895 AddGnuCPlusPlusIncludePaths(
896 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
897 "x86_64-pc-linux-gnu", "", "", triple);
898 // Gentoo amd64 stable
899 AddGnuCPlusPlusIncludePaths(
900 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Douglas Gregor53703832011-03-14 15:33:44 +0000901 "x86_64-pc-linux-gnu", "", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000902
Nick Lewycky66935342010-07-24 21:33:13 +0000903 // Gentoo amd64 llvm-gcc trunk
904 AddGnuCPlusPlusIncludePaths(
905 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
906 "x86_64-pc-linux-gnu", "", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000907
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000908 break;
909 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000910 // FreeBSD 8.0
911 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000912 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000913 break;
Anton Korobeynikovab079412010-08-31 22:39:50 +0000914 case llvm::Triple::NetBSD:
915 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
916 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000917 case llvm::Triple::OpenBSD: {
918 std::string t = triple.getTriple();
919 if (t.substr(0, 6) == "x86_64")
920 t.replace(0, 6, "amd64");
921 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
922 t, "", "", triple);
923 break;
924 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000925 case llvm::Triple::Minix:
926 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
927 "", "", "", triple);
928 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000929 case llvm::Triple::Solaris:
930 // Solaris - Fall though..
931 case llvm::Triple::AuroraUX:
932 // AuroraUX
933 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000934 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000935 break;
936 default:
937 break;
938 }
939}
940
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000941void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000942 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000943 const HeaderSearchOptions &HSOpts) {
Bob Wilson13c4f212011-06-21 21:12:29 +0000944 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes) {
945 if (HSOpts.UseLibcxx)
946 AddPath("/usr/include/c++/v1", CXXSystem, true, false, false);
947 else
Douglas Gregord944a9b2011-06-27 15:47:15 +0000948 AddDefaultCPlusPlusIncludePaths(triple, HSOpts);
Bob Wilson13c4f212011-06-21 21:12:29 +0000949 }
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000950
mike-m79bc57c2010-05-16 19:03:52 +0000951 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000952
953 // Add the default framework include paths on Darwin.
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000954 if (triple.isOSDarwin()) {
Daniel Dunbare1665822009-11-07 04:20:39 +0000955 AddPath("/System/Library/Frameworks", System, true, false, true);
956 AddPath("/Library/Frameworks", System, true, false, true);
957 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000958}
959
Nico Weber0fca0222008-08-22 09:25:22 +0000960/// RemoveDuplicates - If there are duplicate directory entries in the specified
961/// search list, remove the later (dead) ones.
962static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000963 unsigned First, bool Verbose) {
Nico Weber0fca0222008-08-22 09:25:22 +0000964 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
965 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
966 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000967 for (unsigned i = First; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000968 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Chris Lattner43eee072009-02-08 01:00:10 +0000970 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Chris Lattner43eee072009-02-08 01:00:10 +0000972 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000973 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000974 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000975 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000976 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000977 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000978 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000979 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000980 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000981 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000982 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000983 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000984 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Chris Lattner30f05b52009-02-08 00:55:22 +0000987 // If we have a normal #include dir/framework/headermap that is shadowed
988 // later in the chain by a system include location, we actually want to
989 // ignore the user's request and drop the user dir... keeping the system
990 // dir. This is weird, but required to emulate GCC's search path correctly.
991 //
992 // Since dupes of system dirs are rare, just rescan to find the original
993 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000994 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000995 // Find the dir that this is the same of.
996 unsigned FirstDir;
997 for (FirstDir = 0; ; ++FirstDir) {
998 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Chris Lattner43eee072009-02-08 01:00:10 +00001000 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
1001
Chris Lattner30f05b52009-02-08 00:55:22 +00001002 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +00001003 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +00001004 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattner30f05b52009-02-08 00:55:22 +00001006 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +00001007 if (CurEntry.isNormalDir())
1008 isSame = SearchEntry.getDir() == CurEntry.getDir();
1009 else if (CurEntry.isFramework())
1010 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +00001011 else {
Chris Lattner43eee072009-02-08 01:00:10 +00001012 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
1013 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +00001014 }
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Chris Lattner30f05b52009-02-08 00:55:22 +00001016 if (isSame)
1017 break;
1018 }
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Chris Lattner30f05b52009-02-08 00:55:22 +00001020 // If the first dir in the search path is a non-system dir, zap it
1021 // instead of the system one.
1022 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
1023 DirToRemove = FirstDir;
1024 }
1025
1026 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001027 llvm::errs() << "ignoring duplicate directory \""
1028 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +00001029 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001030 llvm::errs() << " as it is a non-system directory that duplicates "
1031 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Chris Lattner7a739402008-09-26 17:46:45 +00001034 // This is reached if the current entry is a duplicate. Remove the
1035 // DirToRemove (usually the current dir).
1036 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +00001037 --i;
1038 }
1039}
1040
1041
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001042void InitHeaderSearch::Realize(const LangOptions &Lang) {
Nico Weber0fca0222008-08-22 09:25:22 +00001043 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
1044 std::vector<DirectoryLookup> SearchList;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001045 SearchList.reserve(IncludePath.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Chris Lattnerebb61642011-06-16 22:56:45 +00001047 // Quoted arguments go first.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001048 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1049 it != ie; ++it) {
1050 if (it->first == Quoted)
1051 SearchList.push_back(it->second);
1052 }
Chris Lattnerebb61642011-06-16 22:56:45 +00001053 // Deduplicate and remember index.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001054 RemoveDuplicates(SearchList, 0, Verbose);
Chris Lattnerebb61642011-06-16 22:56:45 +00001055 unsigned NumQuoted = SearchList.size();
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001057 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1058 it != ie; ++it) {
1059 if (it->first == Angled)
1060 SearchList.push_back(it->second);
1061 }
Chris Lattnerebb61642011-06-16 22:56:45 +00001062
1063 RemoveDuplicates(SearchList, NumQuoted, Verbose);
1064 unsigned NumAngled = SearchList.size();
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001065
1066 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1067 it != ie; ++it) {
1068 if (it->first == System || (Lang.CPlusPlus && it->first == CXXSystem))
1069 SearchList.push_back(it->second);
1070 }
1071
1072 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1073 it != ie; ++it) {
1074 if (it->first == After)
1075 SearchList.push_back(it->second);
1076 }
1077
Chris Lattner1d7f12b2011-06-16 22:58:10 +00001078 // Remove duplicates across both the Angled and System directories. GCC does
1079 // this and failing to remove duplicates across these two groups breaks
1080 // #include_next.
1081 RemoveDuplicates(SearchList, NumQuoted, Verbose);
Nico Weber0fca0222008-08-22 09:25:22 +00001082
1083 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
Chris Lattnerebb61642011-06-16 22:56:45 +00001084 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
Nico Weber0fca0222008-08-22 09:25:22 +00001085
1086 // If verbose, print the list of directories that will be searched.
1087 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001088 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001089 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
Chris Lattnerebb61642011-06-16 22:56:45 +00001090 if (i == NumQuoted)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001091 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001092 const char *Name = SearchList[i].getName();
1093 const char *Suffix;
1094 if (SearchList[i].isNormalDir())
1095 Suffix = "";
1096 else if (SearchList[i].isFramework())
1097 Suffix = " (framework directory)";
1098 else {
1099 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
1100 Suffix = " (headermap)";
1101 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001102 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001103 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001104 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001105 }
1106}
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001107
Daniel Dunbar5814e652009-11-11 21:44:21 +00001108void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
1109 const HeaderSearchOptions &HSOpts,
1110 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001111 const llvm::Triple &Triple) {
1112 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
1113
1114 // Add the user defined entries.
1115 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
1116 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +00001117 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
Bob Wilson5dd45f12011-06-21 21:53:08 +00001118 E.IgnoreSysRoot);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001119 }
1120
1121 // Add entries from CPATH and friends.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001122 Init.AddDelimitedPaths(HSOpts.EnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +00001123 if (Lang.CPlusPlus && Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001124 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +00001125 else if (Lang.CPlusPlus)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001126 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +00001127 else if (Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001128 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +00001129 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001130 Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001131
Daniel Dunbardd35ce92009-11-07 04:58:12 +00001132 if (HSOpts.UseStandardIncludes)
mike-m79bc57c2010-05-16 19:03:52 +00001133 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001134
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001135 Init.Realize(Lang);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001136}