blob: 0b1a2e7a9a6091c7b464d1a8a2eed3314194dc14 [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.
81 void AddMinGW64CXXPaths(llvm::StringRef Base);
82
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000083 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
84 /// separator. The processing follows that of the CPATH variable for gcc.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +000085 void AddDelimitedPaths(llvm::StringRef String);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000086
87 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-m79bc57c2010-05-16 19:03:52 +000088 void AddDefaultCIncludePaths(const llvm::Triple &triple,
89 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000090
91 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
92 // compiling c++.
93 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
94
95 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
96 /// that e.g. stdio.h is found.
97 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +000098 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +000099 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000100
101 /// Realize - Merges all search path lists into one list and send it to
102 /// HeaderSearch.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000103 void Realize(const LangOptions &Lang);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000104};
105
Chris Lattnerebb61642011-06-16 22:56:45 +0000106} // end anonymous namespace.
Nico Weber0fca0222008-08-22 09:25:22 +0000107
Benjamin Kramere89ba592009-12-08 12:38:20 +0000108void InitHeaderSearch::AddPath(const llvm::Twine &Path,
Benjamin Kramer458fb102009-09-05 09:49:39 +0000109 IncludeDirGroup Group, bool isCXXAware,
110 bool isUserSupplied, bool isFramework,
111 bool IgnoreSysRoot) {
Benjamin Kramere89ba592009-12-08 12:38:20 +0000112 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
Nico Weber0fca0222008-08-22 09:25:22 +0000113 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Nico Weber0fca0222008-08-22 09:25:22 +0000115 // Compute the actual path, taking into consideration -isysroot.
Chandler Carruth5853b0f2010-11-15 00:05:18 +0000116 llvm::SmallString<256> MappedPathStorage;
Chandler Carruthf3721452010-11-15 00:48:13 +0000117 llvm::StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Nico Weber0fca0222008-08-22 09:25:22 +0000119 // Handle isysroot.
Rafael Espindola9a7e09d2011-03-02 21:30:07 +0000120 if ((Group == System || Group == CXXSystem) && !IgnoreSysRoot &&
NAKAMURA Takumi0f0cdab2011-05-02 04:50:10 +0000121#if defined(_WIN32)
122 !MappedPathStr.empty() &&
123 llvm::sys::path::is_separator(MappedPathStr[0]) &&
124#else
Michael J. Spencer256053b2010-12-17 21:22:22 +0000125 llvm::sys::path::is_absolute(MappedPathStr) &&
NAKAMURA Takumi0f0cdab2011-05-02 04:50:10 +0000126#endif
Michael J. Spenceraf6530c2010-12-25 20:09:27 +0000127 IsNotEmptyOrRoot) {
Chandler Carruth5619ae52010-11-15 09:28:23 +0000128 MappedPathStorage.clear();
Chandler Carruthc09265a2010-11-15 07:15:26 +0000129 MappedPathStr =
Michael J. Spenceraf6530c2010-12-25 20:09:27 +0000130 (IncludeSysroot + Path).toStringRef(MappedPathStorage);
Nico Weber0fca0222008-08-22 09:25:22 +0000131 }
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Nico Weber0fca0222008-08-22 09:25:22 +0000133 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000134 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000135 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000136 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000137 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000138 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000139 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000140 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
142
Nico Weber0fca0222008-08-22 09:25:22 +0000143 // If the directory exists, add it.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000144 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000145 IncludePath.push_back(std::make_pair(Group, DirectoryLookup(DE, Type,
146 isUserSupplied, isFramework)));
Nico Weber0fca0222008-08-22 09:25:22 +0000147 return;
148 }
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Nico Weber0fca0222008-08-22 09:25:22 +0000150 // Check to see if this is an apple-style headermap (which are not allowed to
151 // be frameworks).
152 if (!isFramework) {
Chris Lattner39b49bc2010-11-23 08:35:12 +0000153 if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000154 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
155 // It is a headermap, add it to the search path.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000156 IncludePath.push_back(std::make_pair(Group, DirectoryLookup(HM, Type,
157 isUserSupplied)));
Nico Weber0fca0222008-08-22 09:25:22 +0000158 return;
159 }
160 }
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Nico Weber0fca0222008-08-22 09:25:22 +0000163 if (Verbose)
Chandler Carruthf3721452010-11-15 00:48:13 +0000164 llvm::errs() << "ignoring nonexistent directory \""
165 << MappedPathStr << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000166}
167
168
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000169void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) {
170 if (at.empty()) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000171 return;
172
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000173 llvm::StringRef::size_type delim;
174 while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) {
175 if (delim == 0)
Nico Weber0fca0222008-08-22 09:25:22 +0000176 AddPath(".", Angled, false, true, false);
177 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000178 AddPath(at.substr(0, delim), Angled, false, true, false);
179 at = at.substr(delim + 1);
Nico Weber0fca0222008-08-22 09:25:22 +0000180 }
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000181
182 if (at.empty())
Nico Weber0fca0222008-08-22 09:25:22 +0000183 AddPath(".", Angled, false, true, false);
184 else
185 AddPath(at, Angled, false, true, false);
186}
187
Benjamin Kramere89ba592009-12-08 12:38:20 +0000188void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
189 llvm::StringRef ArchDir,
190 llvm::StringRef Dir32,
191 llvm::StringRef Dir64,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000192 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000193 // Add the base dir
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000194 AddPath(Base, CXXSystem, true, false, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000195
196 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000197 llvm::Triple::ArchType arch = triple.getArch();
198 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000199 if (is64bit)
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000200 AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000201 else
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000202 AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, true, false, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000203
204 // Add the backward dir
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000205 AddPath(Base + "/backward", CXXSystem, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000206}
Nico Weber0fca0222008-08-22 09:25:22 +0000207
Benjamin Kramere89ba592009-12-08 12:38:20 +0000208void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
209 llvm::StringRef Arch,
210 llvm::StringRef Version) {
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000211 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000212 CXXSystem, true, false, false);
Chris Lattner8e9006b2010-09-03 16:45:53 +0000213 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000214 CXXSystem, true, false, false);
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000215 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000216 CXXSystem, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000217}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000218
NAKAMURA Takumi9db48462011-03-15 02:32:36 +0000219void InitHeaderSearch::AddMinGW64CXXPaths(llvm::StringRef Base) {
220 AddPath(Base,
221 CXXSystem, true, false, false);
222 AddPath(Base + "/x86_64-w64-mingw32",
223 CXXSystem, true, false, false);
224 AddPath(Base + "/backward",
225 CXXSystem, true, false, false);
226}
227
Mike Stump620d57a2009-10-12 20:50:45 +0000228 // FIXME: This probably should goto to some platform utils place.
229#ifdef _MSC_VER
John Thompson75ee3bd2009-11-21 00:15:52 +0000230
Mike Stump620d57a2009-10-12 20:50:45 +0000231 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000232 // This also supports a means to look for high-versioned keys by use
233 // of a $VERSION placeholder in the key path.
234 // $VERSION in the key path is a placeholder for the version number,
235 // causing the highest value path to be searched for and used.
236 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
237 // There can be additional characters in the component. Only the numberic
238 // characters are compared.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000239static bool getSystemRegistryString(const char *keyPath, const char *valueName,
240 char *value, size_t maxLength) {
Mike Stump43d81762009-10-08 23:29:47 +0000241 HKEY hRootKey = NULL;
242 HKEY hKey = NULL;
243 const char* subKey = NULL;
244 DWORD valueType;
245 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000246 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000247 bool returnValue = false;
Chris Lattnerebb61642011-06-16 22:56:45 +0000248
Mike Stump43d81762009-10-08 23:29:47 +0000249 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
250 hRootKey = HKEY_CLASSES_ROOT;
251 subKey = keyPath + 18;
Chris Lattnerebb61642011-06-16 22:56:45 +0000252 } else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
Mike Stump43d81762009-10-08 23:29:47 +0000253 hRootKey = HKEY_USERS;
254 subKey = keyPath + 11;
Chris Lattnerebb61642011-06-16 22:56:45 +0000255 } else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
Mike Stump43d81762009-10-08 23:29:47 +0000256 hRootKey = HKEY_LOCAL_MACHINE;
257 subKey = keyPath + 19;
Chris Lattnerebb61642011-06-16 22:56:45 +0000258 } else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
Mike Stump43d81762009-10-08 23:29:47 +0000259 hRootKey = HKEY_CURRENT_USER;
260 subKey = keyPath + 18;
261 }
262 else
Chris Lattnerebb61642011-06-16 22:56:45 +0000263 return false;
264
John Thompson75ee3bd2009-11-21 00:15:52 +0000265 const char *placeHolder = strstr(subKey, "$VERSION");
266 char bestName[256];
267 bestName[0] = '\0';
268 // If we have a $VERSION placeholder, do the highest-version search.
269 if (placeHolder) {
270 const char *keyEnd = placeHolder - 1;
271 const char *nextKey = placeHolder;
272 // Find end of previous key.
273 while ((keyEnd > subKey) && (*keyEnd != '\\'))
274 keyEnd--;
275 // Find end of key containing $VERSION.
276 while (*nextKey && (*nextKey != '\\'))
277 nextKey++;
278 size_t partialKeyLength = keyEnd - subKey;
279 char partialKey[256];
280 if (partialKeyLength > sizeof(partialKey))
281 partialKeyLength = sizeof(partialKey);
282 strncpy(partialKey, subKey, partialKeyLength);
283 partialKey[partialKeyLength] = '\0';
284 HKEY hTopKey = NULL;
285 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
286 if (lResult == ERROR_SUCCESS) {
287 char keyName[256];
288 int bestIndex = -1;
289 double bestValue = 0.0;
290 DWORD index, size = sizeof(keyName) - 1;
Nuno Lopes33cc2432009-12-07 17:18:48 +0000291 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
292 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
293 const char *sp = keyName;
294 while (*sp && !isdigit(*sp))
295 sp++;
296 if (!*sp)
297 continue;
298 const char *ep = sp + 1;
299 while (*ep && (isdigit(*ep) || (*ep == '.')))
300 ep++;
301 char numBuf[32];
302 strncpy(numBuf, sp, sizeof(numBuf) - 1);
303 numBuf[sizeof(numBuf) - 1] = '\0';
304 double value = strtod(numBuf, NULL);
305 if (value > bestValue) {
306 bestIndex = (int)index;
307 bestValue = value;
308 strcpy(bestName, keyName);
309 }
John Thompson75ee3bd2009-11-21 00:15:52 +0000310 size = sizeof(keyName) - 1;
311 }
312 // If we found the highest versioned key, open the key and get the value.
313 if (bestIndex != -1) {
314 // Append rest of key.
315 strncat(bestName, nextKey, sizeof(bestName) - 1);
316 bestName[sizeof(bestName) - 1] = '\0';
317 // Open the chosen key path remainder.
318 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
319 if (lResult == ERROR_SUCCESS) {
320 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
321 (LPBYTE)value, &valueSize);
322 if (lResult == ERROR_SUCCESS)
323 returnValue = true;
324 RegCloseKey(hKey);
325 }
326 }
327 RegCloseKey(hTopKey);
328 }
329 }
330 else {
331 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
332 if (lResult == ERROR_SUCCESS) {
333 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
334 (LPBYTE)value, &valueSize);
335 if (lResult == ERROR_SUCCESS)
336 returnValue = true;
337 RegCloseKey(hKey);
338 }
Mike Stump43d81762009-10-08 23:29:47 +0000339 }
Chris Lattnerebb61642011-06-16 22:56:45 +0000340 return returnValue;
Mike Stump43d81762009-10-08 23:29:47 +0000341}
Mike Stump620d57a2009-10-12 20:50:45 +0000342#else // _MSC_VER
343 // Read registry string.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000344static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
Mike Stump620d57a2009-10-12 20:50:45 +0000345 return(false);
346}
347#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000348
Mike Stump43d81762009-10-08 23:29:47 +0000349 // Get Visual Studio installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000350static bool getVisualStudioDir(std::string &path) {
Michael J. Spencerff58e362010-08-21 21:55:07 +0000351 // First check the environment variables that vsvars32.bat sets.
352 const char* vcinstalldir = getenv("VCINSTALLDIR");
Chris Lattnerebb61642011-06-16 22:56:45 +0000353 if (vcinstalldir) {
Michael J. Spencerff58e362010-08-21 21:55:07 +0000354 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
355 if (p)
356 *p = '\0';
357 path = vcinstalldir;
Chris Lattnerebb61642011-06-16 22:56:45 +0000358 return true;
Michael J. Spencerff58e362010-08-21 21:55:07 +0000359 }
360
John Thompson75ee3bd2009-11-21 00:15:52 +0000361 char vsIDEInstallDir[256];
Douglas Gregor80f93d92010-05-18 05:47:04 +0000362 char vsExpressIDEInstallDir[256];
Michael J. Spencerff58e362010-08-21 21:55:07 +0000363 // Then try the windows registry.
John Thompson75ee3bd2009-11-21 00:15:52 +0000364 bool hasVCDir = getSystemRegistryString(
365 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
366 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Douglas Gregor80f93d92010-05-18 05:47:04 +0000367 bool hasVCExpressDir = getSystemRegistryString(
368 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
369 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000370 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000371 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000372 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
373 if (p)
374 *p = '\0';
375 path = vsIDEInstallDir;
Chris Lattnerebb61642011-06-16 22:56:45 +0000376 return true;
Mike Stump620d57a2009-10-12 20:50:45 +0000377 }
Chris Lattnerebb61642011-06-16 22:56:45 +0000378
379 if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
Douglas Gregor80f93d92010-05-18 05:47:04 +0000380 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
381 if (p)
382 *p = '\0';
383 path = vsExpressIDEInstallDir;
Chris Lattnerebb61642011-06-16 22:56:45 +0000384 return true;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000385 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000386
Chris Lattnerebb61642011-06-16 22:56:45 +0000387 // Try the environment.
388 const char *vs100comntools = getenv("VS100COMNTOOLS");
389 const char *vs90comntools = getenv("VS90COMNTOOLS");
390 const char *vs80comntools = getenv("VS80COMNTOOLS");
391 const char *vscomntools = NULL;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000392
Chris Lattnerebb61642011-06-16 22:56:45 +0000393 // Try to find the version that we were compiled with
394 if(false) {}
395 #if (_MSC_VER >= 1600) // VC100
396 else if(vs100comntools) {
397 vscomntools = vs100comntools;
Mike Stump43d81762009-10-08 23:29:47 +0000398 }
Chris Lattnerebb61642011-06-16 22:56:45 +0000399 #elif (_MSC_VER == 1500) // VC80
400 else if(vs90comntools) {
401 vscomntools = vs90comntools;
402 }
403 #elif (_MSC_VER == 1400) // VC80
404 else if(vs80comntools) {
405 vscomntools = vs80comntools;
406 }
407 #endif
408 // Otherwise find any version we can
409 else if (vs100comntools)
410 vscomntools = vs100comntools;
411 else if (vs90comntools)
412 vscomntools = vs90comntools;
413 else if (vs80comntools)
414 vscomntools = vs80comntools;
415
416 if (vscomntools && *vscomntools) {
417 char *p = const_cast<char *>(strstr(vscomntools, "\\Common7\\Tools"));
418 if (p)
419 *p = '\0';
420 path = vscomntools;
421 return true;
422 }
423 return false;
Mike Stump43d81762009-10-08 23:29:47 +0000424}
Mike Stump43d81762009-10-08 23:29:47 +0000425
John Thompson75ee3bd2009-11-21 00:15:52 +0000426 // Get Windows SDK installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000427static bool getWindowsSDKDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000428 char windowsSDKInstallDir[256];
429 // Try the Windows registry.
430 bool hasSDKDir = getSystemRegistryString(
431 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
Chris Lattnerebb61642011-06-16 22:56:45 +0000432 "InstallationFolder",
433 windowsSDKInstallDir,
434 sizeof(windowsSDKInstallDir) - 1);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000435 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000436 if (hasSDKDir && windowsSDKInstallDir[0]) {
437 path = windowsSDKInstallDir;
438 return(true);
439 }
440 return(false);
441}
442
mike-m79bc57c2010-05-16 19:03:52 +0000443void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
444 const HeaderSearchOptions &HSOpts) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000445 llvm::Triple::OSType os = triple.getOS();
446
447 switch (os) {
Roman Divacky63076602011-03-01 18:08:03 +0000448 case llvm::Triple::FreeBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000449 case llvm::Triple::NetBSD:
450 break;
451 default:
452 // FIXME: temporary hack: hard-coded paths.
453 AddPath("/usr/local/include", System, true, false, false);
454 break;
455 }
mike-m79bc57c2010-05-16 19:03:52 +0000456
457 // Builtin includes use #include_next directives and should be positioned
458 // just prior C include dirs.
459 if (HSOpts.UseBuiltinIncludes) {
460 // Ignore the sys root, we *always* look for clang headers relative to
461 // supplied path.
462 llvm::sys::Path P(HSOpts.ResourceDir);
463 P.appendComponent("include");
464 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
465 }
466
467 // Add dirs specified via 'configure --with-c-include-dirs'.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000468 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
469 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000470 llvm::SmallVector<llvm::StringRef, 5> dirs;
471 CIncludeDirs.split(dirs, ":");
472 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
473 i != dirs.end();
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000474 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000475 AddPath(*i, System, false, false, false);
476 return;
477 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000478
Mike Stump43d81762009-10-08 23:29:47 +0000479 switch (os) {
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000480 case llvm::Triple::Win32: {
481 std::string VSDir;
482 std::string WindowsSDKDir;
483 if (getVisualStudioDir(VSDir)) {
484 AddPath(VSDir + "\\VC\\include", System, false, false, false);
485 if (getWindowsSDKDir(WindowsSDKDir))
486 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
487 else
488 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
489 System, false, false, false);
490 } else {
491 // Default install paths.
492 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
493 System, false, false, false);
494 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
495 System, false, false, false);
496 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000497 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000498 System, false, false, false);
499 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
500 System, false, false, false);
501 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000502 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000503 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000504 }
505 break;
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000506 }
Chris Lattner86ed3a32010-04-11 19:29:39 +0000507 case llvm::Triple::Haiku:
508 AddPath("/boot/common/include", System, true, false, false);
509 AddPath("/boot/develop/headers/os", System, true, false, false);
510 AddPath("/boot/develop/headers/os/app", System, true, false, false);
511 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
512 AddPath("/boot/develop/headers/os/device", System, true, false, false);
513 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000514 AddPath("/boot/develop/headers/os/game", System, true, false, false);
Chris Lattner86ed3a32010-04-11 19:29:39 +0000515 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
516 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
517 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
518 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
519 AddPath("/boot/develop/headers/os/media", System, true, false, false);
520 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
521 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
522 AddPath("/boot/develop/headers/os/net", System, true, false, false);
523 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
524 AddPath("/boot/develop/headers/os/support", System, true, false, false);
525 AddPath("/boot/develop/headers/os/translation",
526 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000527 AddPath("/boot/develop/headers/os/add-ons/graphics",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000528 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000529 AddPath("/boot/develop/headers/os/add-ons/input_server",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000530 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000531 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
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/tracker",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000534 System, true, false, false);
535 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
536 System, true, false, false);
537 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
538 System, true, false, false);
539 AddPath("/boot/develop/headers/os/be_apps/Tracker",
540 System, true, false, false);
541 AddPath("/boot/develop/headers/cpp", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000542 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000543 System, true, false, false);
544 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
545 AddPath("/boot/develop/headers/bsd", System, true, false, false);
546 AddPath("/boot/develop/headers/glibc", System, true, false, false);
547 AddPath("/boot/develop/headers/posix", System, true, false, false);
548 AddPath("/boot/develop/headers", System, true, false, false);
Eli Friedmana7e68452010-08-22 01:00:03 +0000549 break;
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000550 case llvm::Triple::Cygwin:
551 AddPath("/usr/include/w32api", System, true, false, false);
552 break;
Mike Stump620d57a2009-10-12 20:50:45 +0000553 case llvm::Triple::MinGW32:
NAKAMURA Takumi9db48462011-03-15 02:32:36 +0000554 // FIXME: We should be aware of i686-w64-mingw32.
555 if (triple.getArch() == llvm::Triple::x86_64)
556 AddPath("c:/mingw/x86_64-w64-mingw32/include",
557 System, true, false, false);
Douglas Gregorab0d8bd2011-03-06 18:00:59 +0000558 AddPath("/mingw/include", System, true, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000559 AddPath("c:/mingw/include", System, true, false, false);
560 break;
Eric Christopherb3169da2011-06-03 13:06:30 +0000561 case llvm::Triple::Linux:
562 // Generic Debian multiarch support:
563 if (triple.getArch() == llvm::Triple::x86_64) {
564 AddPath("/usr/include/x86_64-linux-gnu", System, false, false, false);
565 AddPath("/usr/include/i686-linux-gnu/64", System, false, false, false);
566 AddPath("/usr/include/i486-linux-gnu/64", System, false, false, false);
Eric Christopher55ab5b02011-06-03 13:24:15 +0000567 } else if (triple.getArch() == llvm::Triple::x86) {
Eric Christopherb3169da2011-06-03 13:06:30 +0000568 AddPath("/usr/include/x86_64-linux-gnu/32", System, false, false, false);
569 AddPath("/usr/include/i686-linux-gnu", System, false, false, false);
570 AddPath("/usr/include/i486-linux-gnu", System, false, false, false);
Eric Christopher55ab5b02011-06-03 13:24:15 +0000571 } else if (triple.getArch() == llvm::Triple::arm) {
572 AddPath("/usr/include/arm-linux-gnueabi", System, false, false, false);
Eric Christopherb3169da2011-06-03 13:06:30 +0000573 }
Mike Stump43d81762009-10-08 23:29:47 +0000574 default:
Mike Stump43d81762009-10-08 23:29:47 +0000575 break;
576 }
John Thompsond3f88342009-10-13 18:51:32 +0000577
John Thompsond3f88342009-10-13 18:51:32 +0000578 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000579}
580
Chris Lattner0e3cc052010-05-05 05:28:39 +0000581void InitHeaderSearch::
582AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000583 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000584 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
585 if (CxxIncludeRoot != "") {
586 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
587 if (CxxIncludeArch == "")
588 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
Chris Lattner0e3cc052010-05-05 05:28:39 +0000589 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
590 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000591 else
592 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
Chris Lattner0e3cc052010-05-05 05:28:39 +0000593 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
594 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000595 return;
596 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000597 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000598
599 if (triple.isOSDarwin()) {
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000600 switch (triple.getArch()) {
601 default: break;
602
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000603 case llvm::Triple::ppc:
Douglas Gregor582c3012010-05-29 01:15:12 +0000604 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000605 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000606 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor616d4362010-05-29 01:21:11 +0000607 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000608 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000609 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor582c3012010-05-29 01:15:12 +0000610 triple);
611 break;
612
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000613 case llvm::Triple::x86:
614 case llvm::Triple::x86_64:
615 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
616 "i686-apple-darwin10", "", "x86_64", triple);
617 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
618 "i686-apple-darwin8", "", "", triple);
619 break;
620
621 case llvm::Triple::arm:
622 case llvm::Triple::thumb:
623 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
624 "arm-apple-darwin10", "v7", "", triple);
625 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
626 "arm-apple-darwin10", "v6", "", triple);
627 break;
628 }
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000629 return;
630 }
631
632 switch (os) {
633 case llvm::Triple::Cygwin:
634 // Cygwin-1.7
635 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
636 // g++-4 / Cygwin-1.5
637 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
638 // FIXME: Do we support g++-3.4.4?
639 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
640 break;
641 case llvm::Triple::MinGW32:
642 // FIXME: We should be aware of i686-w64-mingw32.
643 if (triple.getArch() == llvm::Triple::x86_64) {
644 // mingw-w64-20110207
645 AddMinGW64CXXPaths("c:/mingw/x86_64-w64-mingw32/include/c++/4.5.3");
646 // mingw-w64-20101129
647 AddMinGW64CXXPaths("c:/mingw/x86_64-w64-mingw32/include/c++/4.5.2");
648 }
649 // Try gcc 4.5.2 (MSYS)
650 AddMinGWCPlusPlusIncludePaths("/mingw/lib/gcc", "mingw32", "4.5.2");
651 // Try gcc 4.5.0
652 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
653 // Try gcc 4.4.0
654 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
655 // Try gcc 4.3.0
656 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000657 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000658 case llvm::Triple::DragonFly:
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000659 AddPath("/usr/include/c++/4.1", CXXSystem, true, false, false);
Chris Lattner7a7ca282010-01-09 05:41:14 +0000660 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000661 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000662 //===------------------------------------------------------------------===//
663 // Debian based distros.
664 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
665 //===------------------------------------------------------------------===//
Eric Christopherb3169da2011-06-03 13:06:30 +0000666
667 // Ubuntu 11.11 "Oneiric Ocelot" -- gcc-4.6.0
668 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
669 "x86_64-linux-gnu", "32", "", triple);
670 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
671 "i686-linux-gnu", "", "64", triple);
672 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
673 "i486-linux-gnu", "", "64", triple);
674 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
675 "arm-linux-gnueabi", "", "", triple);
676
Axel Naumann14a01162011-05-04 09:25:56 +0000677 // Ubuntu 11.04 "Natty Narwhal" -- gcc-4.5.2
678 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
679 "x86_64-linux-gnu", "32", "", triple);
680 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
681 "i686-linux-gnu", "", "64", triple);
682 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
683 "i486-linux-gnu", "", "64", triple);
684 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
685 "arm-linux-gnueabi", "", "", triple);
686
Rafael Espindolac12bbe52011-02-15 21:44:06 +0000687 // Ubuntu 10.10 "Maverick Meerkat" -- gcc-4.4.5
Rafael Espindolaadafdba2011-02-15 21:16:43 +0000688 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
689 "i686-linux-gnu", "", "64", triple);
690 // The rest of 10.10 is the same as previous versions.
691
mike-mac78b7a2010-05-06 14:11:13 +0000692 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
693 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
694 // Debian 6.0 "squeeze" -- gcc-4.4.2
695 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
696 "x86_64-linux-gnu", "32", "", triple);
697 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
698 "i486-linux-gnu", "", "64", triple);
Nick Lewyckydb083552011-02-01 21:32:14 +0000699 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
700 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000701 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
702 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
703 // Debian 5.0 "lenny" -- gcc-4.3.2
704 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
705 "x86_64-linux-gnu", "32", "", triple);
706 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
707 "i486-linux-gnu", "", "64", triple);
Rafael Espindolae7d6c2c2010-06-04 14:28:10 +0000708 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
709 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000710 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
711 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
712 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
713 "x86_64-linux-gnu", "32", "", triple);
714 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
715 "i486-linux-gnu", "", "64", triple);
716 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
717 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
718 "x86_64-linux-gnu", "32", "", triple);
719 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
720 "i486-linux-gnu", "", "64", triple);
721
722 //===------------------------------------------------------------------===//
723 // Redhat based distros.
724 //===------------------------------------------------------------------===//
Eric Christopher8f1cc072011-04-06 18:22:53 +0000725 // Fedora 15
726 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
727 "x86_64-redhat-linux", "32", "", triple);
728 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
729 "i686-redhat-linux", "", "", triple);
Rafael Espindola6638b3a2010-11-02 18:39:34 +0000730 // Fedora 14
731 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
732 "x86_64-redhat-linux", "32", "", triple);
733 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
734 "i686-redhat-linux", "", "", triple);
NAKAMURA Takumic3703982011-06-16 12:43:57 +0000735 // RHEL5(gcc44)
736 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
737 "x86_64-redhat-linux6E", "32", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000738 // Fedora 13
Chris Lattner4336e192010-05-29 01:01:38 +0000739 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
740 "x86_64-redhat-linux", "32", "", triple);
741 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
742 "i686-redhat-linux","", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000743 // Fedora 12
744 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
745 "x86_64-redhat-linux", "32", "", triple);
746 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
747 "i686-redhat-linux","", "", triple);
748 // Fedora 12 (pre-FEB-2010)
749 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
750 "x86_64-redhat-linux", "32", "", triple);
751 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
752 "i686-redhat-linux","", "", triple);
753 // Fedora 11
754 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
755 "x86_64-redhat-linux", "32", "", triple);
756 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
757 "i586-redhat-linux","", "", triple);
758 // Fedora 10
759 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
760 "x86_64-redhat-linux", "32", "", triple);
761 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
762 "i386-redhat-linux","", "", triple);
763 // Fedora 9
764 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
765 "x86_64-redhat-linux", "32", "", triple);
766 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
767 "i386-redhat-linux", "", "", triple);
768 // Fedora 8
769 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
770 "x86_64-redhat-linux", "", "", triple);
771 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
772 "i386-redhat-linux", "", "", triple);
Eric Christopher199e09a2011-05-17 23:06:53 +0000773
774 // RHEL 5
775 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.1",
776 "x86_64-redhat-linux", "32", "", triple);
777 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.1",
778 "i386-redhat-linux", "", "", triple);
779
mike-mac78b7a2010-05-06 14:11:13 +0000780
781 //===------------------------------------------------------------------===//
782
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000783 // Exherbo (2010-01-25)
784 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000785 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000786 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000787 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000788
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000789 // openSUSE 11.1 32 bit
790 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000791 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000792 // openSUSE 11.1 64 bit
793 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000794 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000795 // openSUSE 11.2
796 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000797 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000798 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000799 "x86_64-suse-linux", "", "", triple);
Rafael Espindola9d2c0602010-11-25 18:51:59 +0000800
801 // openSUSE 11.4
802 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
803 "i586-suse-linux", "", "", triple);
804 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
805 "x86_64-suse-linux", "", "", triple);
806
David Chisnall2eb3cce2011-05-19 12:04:49 +0000807 // openSUSE 12.1
808 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
809 "i586-suse-linux", "", "", triple);
810 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
811 "x86_64-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000812 // Arch Linux 2008-06-24
813 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000814 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000815 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000816 "x86_64-unknown-linux-gnu", "", "", triple);
Nico Weber014f9722011-04-25 20:59:30 +0000817
818 // Arch Linux gcc 4.6
819 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
820 "i686-pc-linux-gnu", "", "", triple);
821 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
822 "x86_64-unknown-linux-gnu", "", "", triple);
823
Douglas Gregor53703832011-03-14 15:33:44 +0000824 // Gentoo x86 gcc 4.5.2
825 AddGnuCPlusPlusIncludePaths(
826 "/usr/lib/gcc/i686-pc-linux-gnu/4.5.2/include/g++-v4",
827 "i686-pc-linux-gnu", "", "", triple);
828 // Gentoo x86 gcc 4.4.5
829 AddGnuCPlusPlusIncludePaths(
830 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.5/include/g++-v4",
831 "i686-pc-linux-gnu", "", "", triple);
832 // Gentoo x86 gcc 4.4.4
833 AddGnuCPlusPlusIncludePaths(
834 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.4/include/g++-v4",
835 "i686-pc-linux-gnu", "", "", triple);
836 // Gentoo x86 2010.0 stable
Nuno Lopes0d155a52010-09-11 17:51:45 +0000837 AddGnuCPlusPlusIncludePaths(
838 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
839 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000840 // Gentoo x86 2009.1 stable
841 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000842 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000843 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000844 // Gentoo x86 2009.0 stable
845 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000846 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000847 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000848 // Gentoo x86 2008.0 stable
849 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000850 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000851 "i686-pc-linux-gnu", "", "", triple);
Douglas Gregor53703832011-03-14 15:33:44 +0000852 // Gentoo x86 llvm-gcc trunk
853 AddGnuCPlusPlusIncludePaths(
854 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
855 "i686-pc-linux-gnu", "", "", triple);
Nick Lewycky66935342010-07-24 21:33:13 +0000856
Douglas Gregor53703832011-03-14 15:33:44 +0000857 // Gentoo amd64 gcc 4.5.2
858 AddGnuCPlusPlusIncludePaths(
859 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.2/include/g++-v4",
860 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000861 // Gentoo amd64 gcc 4.4.5
862 AddGnuCPlusPlusIncludePaths(
863 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4",
864 "x86_64-pc-linux-gnu", "32", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000865 // Gentoo amd64 gcc 4.4.4
866 AddGnuCPlusPlusIncludePaths(
867 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
868 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000869 // Gentoo amd64 gcc 4.4.3
870 AddGnuCPlusPlusIncludePaths(
871 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
872 "x86_64-pc-linux-gnu", "32", "", triple);
873 // Gentoo amd64 gcc 4.3.2
874 AddGnuCPlusPlusIncludePaths(
875 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
876 "x86_64-pc-linux-gnu", "", "", triple);
877 // Gentoo amd64 stable
878 AddGnuCPlusPlusIncludePaths(
879 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Douglas Gregor53703832011-03-14 15:33:44 +0000880 "x86_64-pc-linux-gnu", "", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000881
Nick Lewycky66935342010-07-24 21:33:13 +0000882 // Gentoo amd64 llvm-gcc trunk
883 AddGnuCPlusPlusIncludePaths(
884 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
885 "x86_64-pc-linux-gnu", "", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000886
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000887 break;
888 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000889 // FreeBSD 8.0
890 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000891 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000892 break;
Anton Korobeynikovab079412010-08-31 22:39:50 +0000893 case llvm::Triple::NetBSD:
894 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
895 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000896 case llvm::Triple::OpenBSD: {
897 std::string t = triple.getTriple();
898 if (t.substr(0, 6) == "x86_64")
899 t.replace(0, 6, "amd64");
900 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
901 t, "", "", triple);
902 break;
903 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000904 case llvm::Triple::Minix:
905 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
906 "", "", "", triple);
907 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000908 case llvm::Triple::Solaris:
909 // Solaris - Fall though..
910 case llvm::Triple::AuroraUX:
911 // AuroraUX
912 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000913 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000914 break;
915 default:
916 break;
917 }
918}
919
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000920void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000921 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000922 const HeaderSearchOptions &HSOpts) {
Joerg Sonnenberger0bb208c2011-02-22 15:19:35 +0000923 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes)
924 AddDefaultCPlusPlusIncludePaths(triple);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000925
mike-m79bc57c2010-05-16 19:03:52 +0000926 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000927
928 // Add the default framework include paths on Darwin.
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000929 if (triple.isOSDarwin()) {
Daniel Dunbare1665822009-11-07 04:20:39 +0000930 AddPath("/System/Library/Frameworks", System, true, false, true);
931 AddPath("/Library/Frameworks", System, true, false, true);
932 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000933}
934
Nico Weber0fca0222008-08-22 09:25:22 +0000935/// RemoveDuplicates - If there are duplicate directory entries in the specified
936/// search list, remove the later (dead) ones.
937static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000938 unsigned First, bool Verbose) {
Nico Weber0fca0222008-08-22 09:25:22 +0000939 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
940 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
941 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000942 for (unsigned i = First; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000943 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Chris Lattner43eee072009-02-08 01:00:10 +0000945 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Chris Lattner43eee072009-02-08 01:00:10 +0000947 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000948 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000949 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000950 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000951 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000952 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000953 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000954 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000955 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000956 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000957 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000958 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000959 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000960 }
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Chris Lattner30f05b52009-02-08 00:55:22 +0000962 // If we have a normal #include dir/framework/headermap that is shadowed
963 // later in the chain by a system include location, we actually want to
964 // ignore the user's request and drop the user dir... keeping the system
965 // dir. This is weird, but required to emulate GCC's search path correctly.
966 //
967 // Since dupes of system dirs are rare, just rescan to find the original
968 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000969 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000970 // Find the dir that this is the same of.
971 unsigned FirstDir;
972 for (FirstDir = 0; ; ++FirstDir) {
973 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Chris Lattner43eee072009-02-08 01:00:10 +0000975 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
976
Chris Lattner30f05b52009-02-08 00:55:22 +0000977 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000978 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000979 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Chris Lattner30f05b52009-02-08 00:55:22 +0000981 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000982 if (CurEntry.isNormalDir())
983 isSame = SearchEntry.getDir() == CurEntry.getDir();
984 else if (CurEntry.isFramework())
985 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000986 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000987 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
988 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000989 }
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Chris Lattner30f05b52009-02-08 00:55:22 +0000991 if (isSame)
992 break;
993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Chris Lattner30f05b52009-02-08 00:55:22 +0000995 // If the first dir in the search path is a non-system dir, zap it
996 // instead of the system one.
997 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
998 DirToRemove = FirstDir;
999 }
1000
1001 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001002 llvm::errs() << "ignoring duplicate directory \""
1003 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +00001004 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001005 llvm::errs() << " as it is a non-system directory that duplicates "
1006 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Chris Lattner7a739402008-09-26 17:46:45 +00001009 // This is reached if the current entry is a duplicate. Remove the
1010 // DirToRemove (usually the current dir).
1011 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +00001012 --i;
1013 }
1014}
1015
1016
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001017void InitHeaderSearch::Realize(const LangOptions &Lang) {
Nico Weber0fca0222008-08-22 09:25:22 +00001018 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
1019 std::vector<DirectoryLookup> SearchList;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001020 SearchList.reserve(IncludePath.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Chris Lattnerebb61642011-06-16 22:56:45 +00001022 // Quoted arguments go first.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001023 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1024 it != ie; ++it) {
1025 if (it->first == Quoted)
1026 SearchList.push_back(it->second);
1027 }
Chris Lattnerebb61642011-06-16 22:56:45 +00001028 // Deduplicate and remember index.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001029 RemoveDuplicates(SearchList, 0, Verbose);
Chris Lattnerebb61642011-06-16 22:56:45 +00001030 unsigned NumQuoted = SearchList.size();
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001032 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1033 it != ie; ++it) {
1034 if (it->first == Angled)
1035 SearchList.push_back(it->second);
1036 }
Chris Lattnerebb61642011-06-16 22:56:45 +00001037
1038 RemoveDuplicates(SearchList, NumQuoted, Verbose);
1039 unsigned NumAngled = SearchList.size();
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001040
1041 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1042 it != ie; ++it) {
1043 if (it->first == System || (Lang.CPlusPlus && it->first == CXXSystem))
1044 SearchList.push_back(it->second);
1045 }
1046
1047 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1048 it != ie; ++it) {
1049 if (it->first == After)
1050 SearchList.push_back(it->second);
1051 }
1052
Chris Lattnerebb61642011-06-16 22:56:45 +00001053 RemoveDuplicates(SearchList, NumAngled, Verbose);
Nico Weber0fca0222008-08-22 09:25:22 +00001054
1055 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
Chris Lattnerebb61642011-06-16 22:56:45 +00001056 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
Nico Weber0fca0222008-08-22 09:25:22 +00001057
1058 // If verbose, print the list of directories that will be searched.
1059 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001060 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001061 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
Chris Lattnerebb61642011-06-16 22:56:45 +00001062 if (i == NumQuoted)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001063 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001064 const char *Name = SearchList[i].getName();
1065 const char *Suffix;
1066 if (SearchList[i].isNormalDir())
1067 Suffix = "";
1068 else if (SearchList[i].isFramework())
1069 Suffix = " (framework directory)";
1070 else {
1071 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
1072 Suffix = " (headermap)";
1073 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001074 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001075 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +00001076 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +00001077 }
1078}
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001079
Daniel Dunbar5814e652009-11-11 21:44:21 +00001080void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
1081 const HeaderSearchOptions &HSOpts,
1082 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001083 const llvm::Triple &Triple) {
1084 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
1085
1086 // Add the user defined entries.
1087 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
1088 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +00001089 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
Chris Lattner23637be2010-08-24 22:27:37 +00001090 !E.IsSysRootRelative);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001091 }
1092
1093 // Add entries from CPATH and friends.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001094 Init.AddDelimitedPaths(HSOpts.EnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +00001095 if (Lang.CPlusPlus && Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001096 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +00001097 else if (Lang.CPlusPlus)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001098 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +00001099 else if (Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001100 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +00001101 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001102 Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001103
Daniel Dunbardd35ce92009-11-07 04:58:12 +00001104 if (HSOpts.UseStandardIncludes)
mike-m79bc57c2010-05-16 19:03:52 +00001105 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001106
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001107 Init.Realize(Lang);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001108}