blob: 61cc223b3fbf8dd49d9b4bf4050a8b6a94656d8d [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 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
85 /// separator. The processing follows that of the CPATH variable for gcc.
Chris Lattner5f9e2722011-07-23 10:55:15 +000086 void AddDelimitedPaths(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
Chris Lattner5f9e2722011-07-23 10:55:15 +0000110void InitHeaderSearch::AddPath(const 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;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000119 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;
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000137 if (Group == Quoted || Group == Angled || Group == IndexHeaderMap)
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,
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000159 isUserSupplied, Group == IndexHeaderMap)));
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
Chris Lattner5f9e2722011-07-23 10:55:15 +0000171void InitHeaderSearch::AddDelimitedPaths(StringRef at) {
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000172 if (at.empty()) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000173 return;
174
Chris Lattner5f9e2722011-07-23 10:55:15 +0000175 StringRef::size_type delim;
176 while ((delim = at.find(llvm::sys::PathSeparator)) != StringRef::npos) {
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000177 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
Chris Lattner5f9e2722011-07-23 10:55:15 +0000190void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base,
191 StringRef ArchDir,
192 StringRef Dir32,
193 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
Chris Lattner5f9e2722011-07-23 10:55:15 +0000210void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base,
211 StringRef Arch,
212 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
Chris Lattner5f9e2722011-07-23 10:55:15 +0000221void InitHeaderSearch::AddMinGW64CXXPaths(StringRef Base,
222 StringRef Version) {
Douglas Gregord944a9b2011-06-27 15:47:15 +0000223 // 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) {
Francois Picheta6c2b3a2011-07-16 21:17:14 +0000423 const char *p = strstr(vscomntools, "\\Common7\\Tools");
424 path = p ? std::string(vscomntools, p) : vscomntools;
Chris Lattnerebb61642011-06-16 22:56:45 +0000425 return true;
426 }
427 return false;
Mike Stump43d81762009-10-08 23:29:47 +0000428}
Mike Stump43d81762009-10-08 23:29:47 +0000429
John Thompson75ee3bd2009-11-21 00:15:52 +0000430 // Get Windows SDK installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000431static bool getWindowsSDKDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000432 char windowsSDKInstallDir[256];
433 // Try the Windows registry.
434 bool hasSDKDir = getSystemRegistryString(
435 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
Chris Lattnerebb61642011-06-16 22:56:45 +0000436 "InstallationFolder",
437 windowsSDKInstallDir,
438 sizeof(windowsSDKInstallDir) - 1);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000439 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000440 if (hasSDKDir && windowsSDKInstallDir[0]) {
441 path = windowsSDKInstallDir;
442 return(true);
443 }
444 return(false);
445}
446
mike-m79bc57c2010-05-16 19:03:52 +0000447void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
448 const HeaderSearchOptions &HSOpts) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000449 llvm::Triple::OSType os = triple.getOS();
450
451 switch (os) {
Roman Divacky63076602011-03-01 18:08:03 +0000452 case llvm::Triple::FreeBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000453 case llvm::Triple::NetBSD:
454 break;
455 default:
456 // FIXME: temporary hack: hard-coded paths.
457 AddPath("/usr/local/include", System, true, false, false);
458 break;
459 }
mike-m79bc57c2010-05-16 19:03:52 +0000460
461 // Builtin includes use #include_next directives and should be positioned
462 // just prior C include dirs.
463 if (HSOpts.UseBuiltinIncludes) {
464 // Ignore the sys root, we *always* look for clang headers relative to
465 // supplied path.
466 llvm::sys::Path P(HSOpts.ResourceDir);
467 P.appendComponent("include");
468 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
469 }
470
471 // Add dirs specified via 'configure --with-c-include-dirs'.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000472 StringRef CIncludeDirs(C_INCLUDE_DIRS);
Daniel Dunbarc7064682009-11-12 07:28:29 +0000473 if (CIncludeDirs != "") {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000474 SmallVector<StringRef, 5> dirs;
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000475 CIncludeDirs.split(dirs, ":");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000476 for (SmallVectorImpl<StringRef>::iterator i = dirs.begin();
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000477 i != dirs.end();
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000478 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000479 AddPath(*i, System, false, false, false);
480 return;
481 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000482
Mike Stump43d81762009-10-08 23:29:47 +0000483 switch (os) {
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000484 case llvm::Triple::Win32: {
485 std::string VSDir;
486 std::string WindowsSDKDir;
487 if (getVisualStudioDir(VSDir)) {
488 AddPath(VSDir + "\\VC\\include", System, false, false, false);
489 if (getWindowsSDKDir(WindowsSDKDir))
490 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
491 else
492 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
493 System, false, false, false);
494 } else {
495 // Default install paths.
496 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
497 System, false, false, false);
498 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
499 System, false, false, false);
500 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000501 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000502 System, false, false, false);
503 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
504 System, false, false, false);
505 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000506 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000507 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000508 }
509 break;
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000510 }
Chris Lattner86ed3a32010-04-11 19:29:39 +0000511 case llvm::Triple::Haiku:
512 AddPath("/boot/common/include", System, true, false, false);
513 AddPath("/boot/develop/headers/os", System, true, false, false);
514 AddPath("/boot/develop/headers/os/app", System, true, false, false);
515 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
516 AddPath("/boot/develop/headers/os/device", System, true, false, false);
517 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000518 AddPath("/boot/develop/headers/os/game", System, true, false, false);
Chris Lattner86ed3a32010-04-11 19:29:39 +0000519 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
520 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
521 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
522 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
523 AddPath("/boot/develop/headers/os/media", System, true, false, false);
524 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
525 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
526 AddPath("/boot/develop/headers/os/net", System, true, false, false);
527 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
528 AddPath("/boot/develop/headers/os/support", System, true, false, false);
529 AddPath("/boot/develop/headers/os/translation",
530 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000531 AddPath("/boot/develop/headers/os/add-ons/graphics",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000532 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000533 AddPath("/boot/develop/headers/os/add-ons/input_server",
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/screen_saver",
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/tracker",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000538 System, true, false, false);
539 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
540 System, true, false, false);
541 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
542 System, true, false, false);
543 AddPath("/boot/develop/headers/os/be_apps/Tracker",
544 System, true, false, false);
545 AddPath("/boot/develop/headers/cpp", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000546 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000547 System, true, false, false);
548 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
549 AddPath("/boot/develop/headers/bsd", System, true, false, false);
550 AddPath("/boot/develop/headers/glibc", System, true, false, false);
551 AddPath("/boot/develop/headers/posix", System, true, false, false);
552 AddPath("/boot/develop/headers", System, true, false, false);
Eli Friedmana7e68452010-08-22 01:00:03 +0000553 break;
Douglas Gregordca52262011-07-01 22:41:14 +0000554 case llvm::Triple::RTEMS:
555 break;
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000556 case llvm::Triple::Cygwin:
557 AddPath("/usr/include/w32api", System, true, false, false);
558 break;
Douglas Gregord944a9b2011-06-27 15:47:15 +0000559 case llvm::Triple::MinGW32: {
560 // mingw-w64 crt include paths
561 llvm::sys::Path P(HSOpts.ResourceDir);
562 P.appendComponent("../../../i686-w64-mingw32/include"); // <sysroot>/i686-w64-mingw32/include
563 AddPath(P.str(), System, true, false, false);
564 P = llvm::sys::Path(HSOpts.ResourceDir);
565 P.appendComponent("../../../x86_64-w64-mingw32/include"); // <sysroot>/x86_64-w64-mingw32/include
566 AddPath(P.str(), System, true, false, false);
567 // mingw.org crt include paths
568 P = llvm::sys::Path(HSOpts.ResourceDir);
569 P.appendComponent("../../../include"); // <sysroot>/include
570 AddPath(P.str(), System, true, false, false);
571 AddPath("/mingw/include", System, true, false, false);
572 AddPath("c:/mingw/include", System, true, false, false);
573 }
Mike Stump43d81762009-10-08 23:29:47 +0000574 break;
Douglas Gregord944a9b2011-06-27 15:47:15 +0000575
Eric Christopherb3169da2011-06-03 13:06:30 +0000576 case llvm::Triple::Linux:
577 // Generic Debian multiarch support:
578 if (triple.getArch() == llvm::Triple::x86_64) {
579 AddPath("/usr/include/x86_64-linux-gnu", System, false, false, false);
580 AddPath("/usr/include/i686-linux-gnu/64", System, false, false, false);
581 AddPath("/usr/include/i486-linux-gnu/64", System, false, false, false);
Eric Christopher55ab5b02011-06-03 13:24:15 +0000582 } else if (triple.getArch() == llvm::Triple::x86) {
Eric Christopherb3169da2011-06-03 13:06:30 +0000583 AddPath("/usr/include/x86_64-linux-gnu/32", System, false, false, false);
584 AddPath("/usr/include/i686-linux-gnu", System, false, false, false);
585 AddPath("/usr/include/i486-linux-gnu", System, false, false, false);
Eric Christopher55ab5b02011-06-03 13:24:15 +0000586 } else if (triple.getArch() == llvm::Triple::arm) {
587 AddPath("/usr/include/arm-linux-gnueabi", System, false, false, false);
Eric Christopherb3169da2011-06-03 13:06:30 +0000588 }
Mike Stump43d81762009-10-08 23:29:47 +0000589 default:
Mike Stump43d81762009-10-08 23:29:47 +0000590 break;
591 }
John Thompsond3f88342009-10-13 18:51:32 +0000592
Douglas Gregordca52262011-07-01 22:41:14 +0000593 if ( os != llvm::Triple::RTEMS )
594 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000595}
596
Chris Lattner0e3cc052010-05-05 05:28:39 +0000597void InitHeaderSearch::
Douglas Gregord944a9b2011-06-27 15:47:15 +0000598AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000599 llvm::Triple::OSType os = triple.getOS();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000600 StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000601 if (CxxIncludeRoot != "") {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000602 StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000603 if (CxxIncludeArch == "")
604 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
Chris Lattner0e3cc052010-05-05 05:28:39 +0000605 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
606 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000607 else
608 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
Chris Lattner0e3cc052010-05-05 05:28:39 +0000609 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
610 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000611 return;
612 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000613 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000614
615 if (triple.isOSDarwin()) {
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000616 switch (triple.getArch()) {
617 default: break;
618
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000619 case llvm::Triple::ppc:
Douglas Gregor582c3012010-05-29 01:15:12 +0000620 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000621 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000622 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor616d4362010-05-29 01:21:11 +0000623 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000624 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000625 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor582c3012010-05-29 01:15:12 +0000626 triple);
627 break;
628
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000629 case llvm::Triple::x86:
630 case llvm::Triple::x86_64:
631 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
632 "i686-apple-darwin10", "", "x86_64", triple);
633 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
634 "i686-apple-darwin8", "", "", triple);
635 break;
636
637 case llvm::Triple::arm:
638 case llvm::Triple::thumb:
639 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
640 "arm-apple-darwin10", "v7", "", triple);
641 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
642 "arm-apple-darwin10", "v6", "", triple);
643 break;
644 }
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000645 return;
646 }
647
648 switch (os) {
649 case llvm::Triple::Cygwin:
650 // Cygwin-1.7
651 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
652 // g++-4 / Cygwin-1.5
653 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
654 // FIXME: Do we support g++-3.4.4?
655 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
656 break;
657 case llvm::Triple::MinGW32:
Douglas Gregord944a9b2011-06-27 15:47:15 +0000658 // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32)
659 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.0");
660 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.1");
661 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.2");
662 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.3");
663 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.0");
664 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.1");
Douglas Gregorf732f2b2011-07-05 14:16:05 +0000665 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.2");
666 AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0");
Douglas Gregord944a9b2011-06-27 15:47:15 +0000667 // mingw.org C++ include paths
668 AddMinGWCPlusPlusIncludePaths("/mingw/lib/gcc", "mingw32", "4.5.2"); //MSYS
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000669 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000670 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000671 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000672 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000673 case llvm::Triple::DragonFly:
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000674 AddPath("/usr/include/c++/4.1", CXXSystem, true, false, false);
Chris Lattner7a7ca282010-01-09 05:41:14 +0000675 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000676 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000677 //===------------------------------------------------------------------===//
678 // Debian based distros.
679 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
680 //===------------------------------------------------------------------===//
Eric Christopherb3169da2011-06-03 13:06:30 +0000681
682 // Ubuntu 11.11 "Oneiric Ocelot" -- gcc-4.6.0
683 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
684 "x86_64-linux-gnu", "32", "", triple);
685 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
686 "i686-linux-gnu", "", "64", triple);
687 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
688 "i486-linux-gnu", "", "64", triple);
689 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
690 "arm-linux-gnueabi", "", "", triple);
691
Axel Naumann14a01162011-05-04 09:25:56 +0000692 // Ubuntu 11.04 "Natty Narwhal" -- gcc-4.5.2
693 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
694 "x86_64-linux-gnu", "32", "", triple);
695 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
696 "i686-linux-gnu", "", "64", triple);
697 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
698 "i486-linux-gnu", "", "64", triple);
699 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
700 "arm-linux-gnueabi", "", "", triple);
701
Rafael Espindolac12bbe52011-02-15 21:44:06 +0000702 // Ubuntu 10.10 "Maverick Meerkat" -- gcc-4.4.5
Rafael Espindolaadafdba2011-02-15 21:16:43 +0000703 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
704 "i686-linux-gnu", "", "64", triple);
705 // The rest of 10.10 is the same as previous versions.
706
mike-mac78b7a2010-05-06 14:11:13 +0000707 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
708 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
709 // Debian 6.0 "squeeze" -- gcc-4.4.2
710 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
711 "x86_64-linux-gnu", "32", "", triple);
712 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
713 "i486-linux-gnu", "", "64", triple);
Nick Lewyckydb083552011-02-01 21:32:14 +0000714 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
715 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000716 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
717 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
718 // Debian 5.0 "lenny" -- gcc-4.3.2
719 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
720 "x86_64-linux-gnu", "32", "", triple);
721 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
722 "i486-linux-gnu", "", "64", triple);
Rafael Espindolae7d6c2c2010-06-04 14:28:10 +0000723 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
724 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000725 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
726 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
727 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
728 "x86_64-linux-gnu", "32", "", triple);
729 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
730 "i486-linux-gnu", "", "64", triple);
731 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
732 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
733 "x86_64-linux-gnu", "32", "", triple);
734 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
735 "i486-linux-gnu", "", "64", triple);
736
737 //===------------------------------------------------------------------===//
738 // Redhat based distros.
739 //===------------------------------------------------------------------===//
Eric Christopher8f1cc072011-04-06 18:22:53 +0000740 // Fedora 15
741 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
742 "x86_64-redhat-linux", "32", "", triple);
743 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
744 "i686-redhat-linux", "", "", triple);
Rafael Espindola6638b3a2010-11-02 18:39:34 +0000745 // Fedora 14
746 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
747 "x86_64-redhat-linux", "32", "", triple);
748 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
749 "i686-redhat-linux", "", "", triple);
NAKAMURA Takumic3703982011-06-16 12:43:57 +0000750 // RHEL5(gcc44)
751 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
752 "x86_64-redhat-linux6E", "32", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000753 // Fedora 13
Chris Lattner4336e192010-05-29 01:01:38 +0000754 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
755 "x86_64-redhat-linux", "32", "", triple);
756 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
757 "i686-redhat-linux","", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000758 // Fedora 12
759 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
760 "x86_64-redhat-linux", "32", "", triple);
761 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
762 "i686-redhat-linux","", "", triple);
763 // Fedora 12 (pre-FEB-2010)
764 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
765 "x86_64-redhat-linux", "32", "", triple);
766 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
767 "i686-redhat-linux","", "", triple);
768 // Fedora 11
769 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
770 "x86_64-redhat-linux", "32", "", triple);
771 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
772 "i586-redhat-linux","", "", triple);
773 // Fedora 10
774 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
775 "x86_64-redhat-linux", "32", "", triple);
776 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
777 "i386-redhat-linux","", "", triple);
778 // Fedora 9
779 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
780 "x86_64-redhat-linux", "32", "", triple);
781 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
782 "i386-redhat-linux", "", "", triple);
783 // Fedora 8
784 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
785 "x86_64-redhat-linux", "", "", triple);
786 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
787 "i386-redhat-linux", "", "", triple);
Eric Christopher199e09a2011-05-17 23:06:53 +0000788
789 // RHEL 5
790 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.1",
791 "x86_64-redhat-linux", "32", "", triple);
792 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.1",
793 "i386-redhat-linux", "", "", triple);
794
mike-mac78b7a2010-05-06 14:11:13 +0000795
796 //===------------------------------------------------------------------===//
797
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000798 // Exherbo (2010-01-25)
799 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000800 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000801 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000802 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000803
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000804 // openSUSE 11.1 32 bit
805 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000806 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000807 // openSUSE 11.1 64 bit
808 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000809 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000810 // openSUSE 11.2
811 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000812 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000813 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000814 "x86_64-suse-linux", "", "", triple);
Rafael Espindola9d2c0602010-11-25 18:51:59 +0000815
816 // openSUSE 11.4
817 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
818 "i586-suse-linux", "", "", triple);
819 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
820 "x86_64-suse-linux", "", "", triple);
821
David Chisnall2eb3cce2011-05-19 12:04:49 +0000822 // openSUSE 12.1
823 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
824 "i586-suse-linux", "", "", triple);
825 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
826 "x86_64-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000827 // Arch Linux 2008-06-24
828 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000829 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000830 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000831 "x86_64-unknown-linux-gnu", "", "", triple);
Nico Weber014f9722011-04-25 20:59:30 +0000832
833 // Arch Linux gcc 4.6
Chandler Carruth37187cc2011-07-02 00:51:03 +0000834 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
835 "i686-pc-linux-gnu", "", "", triple);
836 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
837 "x86_64-unknown-linux-gnu", "", "", triple);
Nico Weber014f9722011-04-25 20:59:30 +0000838 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
839 "i686-pc-linux-gnu", "", "", triple);
840 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
841 "x86_64-unknown-linux-gnu", "", "", triple);
842
Douglas Gregor53703832011-03-14 15:33:44 +0000843 // Gentoo x86 gcc 4.5.2
844 AddGnuCPlusPlusIncludePaths(
845 "/usr/lib/gcc/i686-pc-linux-gnu/4.5.2/include/g++-v4",
846 "i686-pc-linux-gnu", "", "", triple);
847 // Gentoo x86 gcc 4.4.5
848 AddGnuCPlusPlusIncludePaths(
849 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.5/include/g++-v4",
850 "i686-pc-linux-gnu", "", "", triple);
851 // Gentoo x86 gcc 4.4.4
852 AddGnuCPlusPlusIncludePaths(
853 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.4/include/g++-v4",
854 "i686-pc-linux-gnu", "", "", triple);
855 // Gentoo x86 2010.0 stable
Nuno Lopes0d155a52010-09-11 17:51:45 +0000856 AddGnuCPlusPlusIncludePaths(
857 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
858 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000859 // Gentoo x86 2009.1 stable
860 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000861 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000862 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000863 // Gentoo x86 2009.0 stable
864 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000865 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000866 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000867 // Gentoo x86 2008.0 stable
868 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000869 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000870 "i686-pc-linux-gnu", "", "", triple);
Douglas Gregor53703832011-03-14 15:33:44 +0000871 // Gentoo x86 llvm-gcc trunk
872 AddGnuCPlusPlusIncludePaths(
873 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
874 "i686-pc-linux-gnu", "", "", triple);
Nick Lewycky66935342010-07-24 21:33:13 +0000875
Douglas Gregor53703832011-03-14 15:33:44 +0000876 // Gentoo amd64 gcc 4.5.2
877 AddGnuCPlusPlusIncludePaths(
878 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.2/include/g++-v4",
879 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000880 // Gentoo amd64 gcc 4.4.5
881 AddGnuCPlusPlusIncludePaths(
882 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4",
883 "x86_64-pc-linux-gnu", "32", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000884 // Gentoo amd64 gcc 4.4.4
885 AddGnuCPlusPlusIncludePaths(
886 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
887 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000888 // Gentoo amd64 gcc 4.4.3
889 AddGnuCPlusPlusIncludePaths(
890 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
891 "x86_64-pc-linux-gnu", "32", "", triple);
892 // Gentoo amd64 gcc 4.3.2
893 AddGnuCPlusPlusIncludePaths(
894 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
895 "x86_64-pc-linux-gnu", "", "", triple);
896 // Gentoo amd64 stable
897 AddGnuCPlusPlusIncludePaths(
898 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Douglas Gregor53703832011-03-14 15:33:44 +0000899 "x86_64-pc-linux-gnu", "", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000900
Nick Lewycky66935342010-07-24 21:33:13 +0000901 // Gentoo amd64 llvm-gcc trunk
902 AddGnuCPlusPlusIncludePaths(
903 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
904 "x86_64-pc-linux-gnu", "", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000905
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000906 break;
907 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000908 // FreeBSD 8.0
909 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000910 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000911 break;
Anton Korobeynikovab079412010-08-31 22:39:50 +0000912 case llvm::Triple::NetBSD:
913 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
914 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000915 case llvm::Triple::OpenBSD: {
916 std::string t = triple.getTriple();
917 if (t.substr(0, 6) == "x86_64")
918 t.replace(0, 6, "amd64");
919 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
920 t, "", "", triple);
921 break;
922 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000923 case llvm::Triple::Minix:
924 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
925 "", "", "", triple);
926 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000927 case llvm::Triple::Solaris:
928 // Solaris - Fall though..
929 case llvm::Triple::AuroraUX:
930 // AuroraUX
931 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000932 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000933 break;
934 default:
935 break;
936 }
937}
938
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000939void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000940 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000941 const HeaderSearchOptions &HSOpts) {
Bob Wilson13c4f212011-06-21 21:12:29 +0000942 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes) {
Douglas Gregorbaf41f12011-07-29 20:21:18 +0000943 if (HSOpts.UseLibcxx) {
944 if (triple.isOSDarwin()) {
945 // On Darwin, libc++ may be installed alongside the compiler in
946 // lib/c++/v1.
947 llvm::sys::Path P(HSOpts.ResourceDir);
948 if (!P.isEmpty()) {
949 P.eraseComponent(); // Remove version from foo/lib/clang/version
950 P.eraseComponent(); // Remove clang from foo/lib/clang
951
952 // Get foo/lib/c++/v1
953 P.appendComponent("c++");
954 P.appendComponent("v1");
955 AddPath(P.str(), CXXSystem, true, false, false, true);
956 }
957 }
958
Bob Wilson13c4f212011-06-21 21:12:29 +0000959 AddPath("/usr/include/c++/v1", CXXSystem, true, false, false);
Douglas Gregorbaf41f12011-07-29 20:21:18 +0000960 }
Bob Wilson13c4f212011-06-21 21:12:29 +0000961 else
Douglas Gregord944a9b2011-06-27 15:47:15 +0000962 AddDefaultCPlusPlusIncludePaths(triple, HSOpts);
Bob Wilson13c4f212011-06-21 21:12:29 +0000963 }
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000964
mike-m79bc57c2010-05-16 19:03:52 +0000965 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000966
967 // Add the default framework include paths on Darwin.
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000968 if (triple.isOSDarwin()) {
Daniel Dunbare1665822009-11-07 04:20:39 +0000969 AddPath("/System/Library/Frameworks", System, true, false, true);
970 AddPath("/Library/Frameworks", System, true, false, true);
971 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000972}
973
Nico Weber0fca0222008-08-22 09:25:22 +0000974/// RemoveDuplicates - If there are duplicate directory entries in the specified
975/// search list, remove the later (dead) ones.
976static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000977 unsigned First, bool Verbose) {
Nico Weber0fca0222008-08-22 09:25:22 +0000978 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
979 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
980 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000981 for (unsigned i = First; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000982 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Chris Lattner43eee072009-02-08 01:00:10 +0000984 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Chris Lattner43eee072009-02-08 01:00:10 +0000986 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000987 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000988 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000989 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000990 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000991 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000992 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000993 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000994 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000995 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000996 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000997 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000998 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Chris Lattner30f05b52009-02-08 00:55:22 +00001001 // If we have a normal #include dir/framework/headermap that is shadowed
1002 // later in the chain by a system include location, we actually want to
1003 // ignore the user's request and drop the user dir... keeping the system
1004 // dir. This is weird, but required to emulate GCC's search path correctly.
1005 //
1006 // Since dupes of system dirs are rare, just rescan to find the original
1007 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +00001008 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +00001009 // Find the dir that this is the same of.
1010 unsigned FirstDir;
1011 for (FirstDir = 0; ; ++FirstDir) {
1012 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Chris Lattner43eee072009-02-08 01:00:10 +00001014 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
1015
Chris Lattner30f05b52009-02-08 00:55:22 +00001016 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +00001017 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +00001018 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Chris Lattner30f05b52009-02-08 00:55:22 +00001020 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +00001021 if (CurEntry.isNormalDir())
1022 isSame = SearchEntry.getDir() == CurEntry.getDir();
1023 else if (CurEntry.isFramework())
1024 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +00001025 else {
Chris Lattner43eee072009-02-08 01:00:10 +00001026 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
1027 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +00001028 }
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Chris Lattner30f05b52009-02-08 00:55:22 +00001030 if (isSame)
1031 break;
1032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Chris Lattner30f05b52009-02-08 00:55:22 +00001034 // If the first dir in the search path is a non-system dir, zap it
1035 // instead of the system one.
1036 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
1037 DirToRemove = FirstDir;
1038 }
1039
1040 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001041 llvm::errs() << "ignoring duplicate directory \""
1042 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +00001043 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001044 llvm::errs() << " as it is a non-system directory that duplicates "
1045 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001046 }
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Chris Lattner7a739402008-09-26 17:46:45 +00001048 // This is reached if the current entry is a duplicate. Remove the
1049 // DirToRemove (usually the current dir).
1050 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +00001051 --i;
1052 }
1053}
1054
1055
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001056void InitHeaderSearch::Realize(const LangOptions &Lang) {
Nico Weber0fca0222008-08-22 09:25:22 +00001057 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
1058 std::vector<DirectoryLookup> SearchList;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001059 SearchList.reserve(IncludePath.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Chris Lattnerebb61642011-06-16 22:56:45 +00001061 // Quoted arguments go first.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001062 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1063 it != ie; ++it) {
1064 if (it->first == Quoted)
1065 SearchList.push_back(it->second);
1066 }
Chris Lattnerebb61642011-06-16 22:56:45 +00001067 // Deduplicate and remember index.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001068 RemoveDuplicates(SearchList, 0, Verbose);
Chris Lattnerebb61642011-06-16 22:56:45 +00001069 unsigned NumQuoted = SearchList.size();
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001071 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1072 it != ie; ++it) {
Douglas Gregor65e02fa2011-07-28 04:45:53 +00001073 if (it->first == Angled || it->first == IndexHeaderMap)
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001074 SearchList.push_back(it->second);
1075 }
Chris Lattnerebb61642011-06-16 22:56:45 +00001076
1077 RemoveDuplicates(SearchList, NumQuoted, Verbose);
1078 unsigned NumAngled = SearchList.size();
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001079
1080 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1081 it != ie; ++it) {
1082 if (it->first == System || (Lang.CPlusPlus && it->first == CXXSystem))
1083 SearchList.push_back(it->second);
1084 }
1085
1086 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1087 it != ie; ++it) {
1088 if (it->first == After)
1089 SearchList.push_back(it->second);
1090 }
1091
Chris Lattner1d7f12b2011-06-16 22:58:10 +00001092 // Remove duplicates across both the Angled and System directories. GCC does
1093 // this and failing to remove duplicates across these two groups breaks
1094 // #include_next.
1095 RemoveDuplicates(SearchList, NumQuoted, Verbose);
Nico Weber0fca0222008-08-22 09:25:22 +00001096
1097 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
Chris Lattnerebb61642011-06-16 22:56:45 +00001098 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
Nico Weber0fca0222008-08-22 09:25:22 +00001099
1100 // If verbose, print the list of directories that will be searched.
1101 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001102 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001103 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
Chris Lattnerebb61642011-06-16 22:56:45 +00001104 if (i == NumQuoted)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001105 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001106 const char *Name = SearchList[i].getName();
1107 const char *Suffix;
1108 if (SearchList[i].isNormalDir())
1109 Suffix = "";
1110 else if (SearchList[i].isFramework())
1111 Suffix = " (framework directory)";
1112 else {
1113 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
1114 Suffix = " (headermap)";
1115 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001116 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001117 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001118 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001119 }
1120}
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001121
Daniel Dunbar5814e652009-11-11 21:44:21 +00001122void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
1123 const HeaderSearchOptions &HSOpts,
1124 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001125 const llvm::Triple &Triple) {
1126 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
1127
1128 // Add the user defined entries.
1129 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
1130 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +00001131 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
Bob Wilson5dd45f12011-06-21 21:53:08 +00001132 E.IgnoreSysRoot);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001133 }
1134
1135 // Add entries from CPATH and friends.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001136 Init.AddDelimitedPaths(HSOpts.EnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +00001137 if (Lang.CPlusPlus && Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001138 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +00001139 else if (Lang.CPlusPlus)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001140 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +00001141 else if (Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001142 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +00001143 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001144 Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001145
Daniel Dunbardd35ce92009-11-07 04:58:12 +00001146 if (HSOpts.UseStandardIncludes)
mike-m79bc57c2010-05-16 19:03:52 +00001147 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001148
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001149 Init.Realize(Lang);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001150}