blob: f10e802ed9e6c8594a2c6764aeb5f76f5da06bfd [file] [log] [blame]
Nico Weber0fca0222008-08-22 09:25:22 +00001//===--- InitHeaderSearch.cpp - Initialize header search paths ----------*-===//
2//
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
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000014#include "clang/Frontend/Utils.h"
Nico Weber0fca0222008-08-22 09:25:22 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/LangOptions.h"
Daniel Dunbar63c8b772009-11-07 04:20:50 +000017#include "clang/Frontend/HeaderSearchOptions.h"
18#include "clang/Lex/HeaderSearch.h"
Nico Weber0fca0222008-08-22 09:25:22 +000019#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/SmallPtrSet.h"
Rafael Espindolaaadd7a42009-11-13 05:13:58 +000021#include "llvm/ADT/SmallVector.h"
Rafael Espindolaf0a2f512009-11-12 05:48:41 +000022#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000023#include "llvm/ADT/Triple.h"
Benjamin Kramere89ba592009-12-08 12:38:20 +000024#include "llvm/ADT/Twine.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000025#include "llvm/Support/raw_ostream.h"
Nico Weber0fca0222008-08-22 09:25:22 +000026#include "llvm/System/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000027#include "llvm/Config/config.h"
Mike Stump620d57a2009-10-12 20:50:45 +000028#ifdef _MSC_VER
29 #define WIN32_LEAN_AND_MEAN 1
30 #include <windows.h>
31#endif
Nico Weber0fca0222008-08-22 09:25:22 +000032using namespace clang;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000033using namespace clang::frontend;
34
35namespace {
36
37/// InitHeaderSearch - This class makes it easier to set the search paths of
38/// a HeaderSearch object. InitHeaderSearch stores several search path lists
39/// internally, which can be sent to a HeaderSearch object in one swoop.
40class InitHeaderSearch {
41 std::vector<DirectoryLookup> IncludeGroup[4];
42 HeaderSearch& Headers;
43 bool Verbose;
44 std::string isysroot;
45
46public:
47
48 InitHeaderSearch(HeaderSearch &HS,
49 bool verbose = false, const std::string &iSysroot = "")
50 : Headers(HS), Verbose(verbose), isysroot(iSysroot) {}
51
52 /// AddPath - Add the specified path to the specified group list.
Benjamin Kramere89ba592009-12-08 12:38:20 +000053 void AddPath(const llvm::Twine &Path, IncludeDirGroup Group,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000054 bool isCXXAware, bool isUserSupplied,
55 bool isFramework, bool IgnoreSysRoot = false);
56
mike-ma6087372010-05-05 17:00:31 +000057 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000058 /// libstdc++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000059 void AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
60 llvm::StringRef ArchDir,
61 llvm::StringRef Dir32,
62 llvm::StringRef Dir64,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000063 const llvm::Triple &triple);
64
65 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW
66 /// libstdc++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000067 void AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
68 llvm::StringRef Arch,
69 llvm::StringRef Version);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000070
71 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
72 /// separator. The processing follows that of the CPATH variable for gcc.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +000073 void AddDelimitedPaths(llvm::StringRef String);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000074
75 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-mc3ec9ae2010-05-06 15:23:52 +000076 void AddDefaultCIncludePaths(const llvm::Triple &triple);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000077
78 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
79 // compiling c++.
80 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
81
82 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
83 /// that e.g. stdio.h is found.
84 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +000085 const llvm::Triple &triple,
mike-mc3ec9ae2010-05-06 15:23:52 +000086 bool UseStandardCXXIncludes);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000087
88 /// Realize - Merges all search path lists into one list and send it to
89 /// HeaderSearch.
90 void Realize();
91};
92
93}
Nico Weber0fca0222008-08-22 09:25:22 +000094
Benjamin Kramere89ba592009-12-08 12:38:20 +000095void InitHeaderSearch::AddPath(const llvm::Twine &Path,
Benjamin Kramer458fb102009-09-05 09:49:39 +000096 IncludeDirGroup Group, bool isCXXAware,
97 bool isUserSupplied, bool isFramework,
98 bool IgnoreSysRoot) {
Benjamin Kramere89ba592009-12-08 12:38:20 +000099 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
Nico Weber0fca0222008-08-22 09:25:22 +0000100 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Nico Weber0fca0222008-08-22 09:25:22 +0000102 // Compute the actual path, taking into consideration -isysroot.
Benjamin Kramere89ba592009-12-08 12:38:20 +0000103 llvm::SmallString<256> MappedPathStr;
104 llvm::raw_svector_ostream MappedPath(MappedPathStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Nico Weber0fca0222008-08-22 09:25:22 +0000106 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +0000107 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +0000108 // FIXME: Portability. This should be a sys::Path interface, this doesn't
109 // handle things like C:\ right, nor win32 \\network\device\blah.
110 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
Benjamin Kramere89ba592009-12-08 12:38:20 +0000111 MappedPath << isysroot;
Nico Weber0fca0222008-08-22 09:25:22 +0000112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Benjamin Kramere89ba592009-12-08 12:38:20 +0000114 Path.print(MappedPath);
Nico Weber0fca0222008-08-22 09:25:22 +0000115
116 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000117 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000118 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000119 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000120 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000121 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000122 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000123 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000124
125
Nico Weber0fca0222008-08-22 09:25:22 +0000126 // If the directory exists, add it.
Benjamin Kramer458fb102009-09-05 09:49:39 +0000127 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000128 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
129 isFramework));
130 return;
131 }
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Nico Weber0fca0222008-08-22 09:25:22 +0000133 // Check to see if this is an apple-style headermap (which are not allowed to
134 // be frameworks).
135 if (!isFramework) {
Benjamin Kramer458fb102009-09-05 09:49:39 +0000136 if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000137 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
138 // It is a headermap, add it to the search path.
139 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
140 return;
141 }
142 }
143 }
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Nico Weber0fca0222008-08-22 09:25:22 +0000145 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +0000146 llvm::errs() << "ignoring nonexistent directory \""
147 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000148}
149
150
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000151void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) {
152 if (at.empty()) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000153 return;
154
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000155 llvm::StringRef::size_type delim;
156 while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) {
157 if (delim == 0)
Nico Weber0fca0222008-08-22 09:25:22 +0000158 AddPath(".", Angled, false, true, false);
159 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000160 AddPath(at.substr(0, delim), Angled, false, true, false);
161 at = at.substr(delim + 1);
Nico Weber0fca0222008-08-22 09:25:22 +0000162 }
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000163
164 if (at.empty())
Nico Weber0fca0222008-08-22 09:25:22 +0000165 AddPath(".", Angled, false, true, false);
166 else
167 AddPath(at, Angled, false, true, false);
168}
169
Benjamin Kramere89ba592009-12-08 12:38:20 +0000170void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
171 llvm::StringRef ArchDir,
172 llvm::StringRef Dir32,
173 llvm::StringRef Dir64,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000174 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000175 // Add the base dir
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000176 AddPath(Base, System, true, false, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000177
178 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000179 llvm::Triple::ArchType arch = triple.getArch();
180 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000181 if (is64bit)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000182 AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000183 else
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000184 AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000185
186 // Add the backward dir
187 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000188}
Nico Weber0fca0222008-08-22 09:25:22 +0000189
Benjamin Kramere89ba592009-12-08 12:38:20 +0000190void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
191 llvm::StringRef Arch,
192 llvm::StringRef Version) {
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000193 AddPath(Base + "/" + Arch + "/" + Version + "/include",
194 System, true, false, false);
195 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
196 System, true, false, false);
197 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
198 System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000199}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000200
Mike Stump620d57a2009-10-12 20:50:45 +0000201 // FIXME: This probably should goto to some platform utils place.
202#ifdef _MSC_VER
John Thompson75ee3bd2009-11-21 00:15:52 +0000203
Mike Stump620d57a2009-10-12 20:50:45 +0000204 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000205 // This also supports a means to look for high-versioned keys by use
206 // of a $VERSION placeholder in the key path.
207 // $VERSION in the key path is a placeholder for the version number,
208 // causing the highest value path to be searched for and used.
209 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
210 // There can be additional characters in the component. Only the numberic
211 // characters are compared.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000212static bool getSystemRegistryString(const char *keyPath, const char *valueName,
213 char *value, size_t maxLength) {
Mike Stump43d81762009-10-08 23:29:47 +0000214 HKEY hRootKey = NULL;
215 HKEY hKey = NULL;
216 const char* subKey = NULL;
217 DWORD valueType;
218 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000219 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000220 bool returnValue = false;
221 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
222 hRootKey = HKEY_CLASSES_ROOT;
223 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000224 }
Mike Stump43d81762009-10-08 23:29:47 +0000225 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
226 hRootKey = HKEY_USERS;
227 subKey = keyPath + 11;
228 }
229 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
230 hRootKey = HKEY_LOCAL_MACHINE;
231 subKey = keyPath + 19;
232 }
233 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
234 hRootKey = HKEY_CURRENT_USER;
235 subKey = keyPath + 18;
236 }
237 else
238 return(false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000239 const char *placeHolder = strstr(subKey, "$VERSION");
240 char bestName[256];
241 bestName[0] = '\0';
242 // If we have a $VERSION placeholder, do the highest-version search.
243 if (placeHolder) {
244 const char *keyEnd = placeHolder - 1;
245 const char *nextKey = placeHolder;
246 // Find end of previous key.
247 while ((keyEnd > subKey) && (*keyEnd != '\\'))
248 keyEnd--;
249 // Find end of key containing $VERSION.
250 while (*nextKey && (*nextKey != '\\'))
251 nextKey++;
252 size_t partialKeyLength = keyEnd - subKey;
253 char partialKey[256];
254 if (partialKeyLength > sizeof(partialKey))
255 partialKeyLength = sizeof(partialKey);
256 strncpy(partialKey, subKey, partialKeyLength);
257 partialKey[partialKeyLength] = '\0';
258 HKEY hTopKey = NULL;
259 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
260 if (lResult == ERROR_SUCCESS) {
261 char keyName[256];
262 int bestIndex = -1;
263 double bestValue = 0.0;
264 DWORD index, size = sizeof(keyName) - 1;
Nuno Lopes33cc2432009-12-07 17:18:48 +0000265 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
266 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
267 const char *sp = keyName;
268 while (*sp && !isdigit(*sp))
269 sp++;
270 if (!*sp)
271 continue;
272 const char *ep = sp + 1;
273 while (*ep && (isdigit(*ep) || (*ep == '.')))
274 ep++;
275 char numBuf[32];
276 strncpy(numBuf, sp, sizeof(numBuf) - 1);
277 numBuf[sizeof(numBuf) - 1] = '\0';
278 double value = strtod(numBuf, NULL);
279 if (value > bestValue) {
280 bestIndex = (int)index;
281 bestValue = value;
282 strcpy(bestName, keyName);
283 }
John Thompson75ee3bd2009-11-21 00:15:52 +0000284 size = sizeof(keyName) - 1;
285 }
286 // If we found the highest versioned key, open the key and get the value.
287 if (bestIndex != -1) {
288 // Append rest of key.
289 strncat(bestName, nextKey, sizeof(bestName) - 1);
290 bestName[sizeof(bestName) - 1] = '\0';
291 // Open the chosen key path remainder.
292 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
293 if (lResult == ERROR_SUCCESS) {
294 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
295 (LPBYTE)value, &valueSize);
296 if (lResult == ERROR_SUCCESS)
297 returnValue = true;
298 RegCloseKey(hKey);
299 }
300 }
301 RegCloseKey(hTopKey);
302 }
303 }
304 else {
305 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
306 if (lResult == ERROR_SUCCESS) {
307 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
308 (LPBYTE)value, &valueSize);
309 if (lResult == ERROR_SUCCESS)
310 returnValue = true;
311 RegCloseKey(hKey);
312 }
Mike Stump43d81762009-10-08 23:29:47 +0000313 }
314 return(returnValue);
315}
Mike Stump620d57a2009-10-12 20:50:45 +0000316#else // _MSC_VER
317 // Read registry string.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000318static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
Mike Stump620d57a2009-10-12 20:50:45 +0000319 return(false);
320}
321#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000322
Mike Stump43d81762009-10-08 23:29:47 +0000323 // Get Visual Studio installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000324static bool getVisualStudioDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000325 char vsIDEInstallDir[256];
Mike Stump620d57a2009-10-12 20:50:45 +0000326 // Try the Windows registry first.
John Thompson75ee3bd2009-11-21 00:15:52 +0000327 bool hasVCDir = getSystemRegistryString(
328 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
329 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000330 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000331 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000332 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
333 if (p)
334 *p = '\0';
335 path = vsIDEInstallDir;
336 return(true);
337 }
338 else {
339 // Try the environment.
340 const char* vs90comntools = getenv("VS90COMNTOOLS");
341 const char* vs80comntools = getenv("VS80COMNTOOLS");
342 const char* vscomntools = NULL;
343 // If we have both vc80 and vc90, pick version we were compiled with.
344 if (vs90comntools && vs80comntools) {
345 #if (_MSC_VER >= 1500) // VC90
Mike Stump43d81762009-10-08 23:29:47 +0000346 vscomntools = vs90comntools;
347 #elif (_MSC_VER == 1400) // VC80
348 vscomntools = vs80comntools;
349 #else
350 vscomntools = vs90comntools;
351 #endif
Mike Stump620d57a2009-10-12 20:50:45 +0000352 }
353 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000354 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000355 else if (vs80comntools)
356 vscomntools = vs80comntools;
357 if (vscomntools && *vscomntools) {
Dan Gohmancb421fa2010-04-19 16:39:44 +0000358 char *p = const_cast<char *>(strstr(vscomntools, "\\Common7\\Tools"));
Mike Stump620d57a2009-10-12 20:50:45 +0000359 if (p)
360 *p = '\0';
361 path = vscomntools;
362 return(true);
363 }
364 else
365 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000366 }
Mike Stump620d57a2009-10-12 20:50:45 +0000367 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000368}
Mike Stump43d81762009-10-08 23:29:47 +0000369
John Thompson75ee3bd2009-11-21 00:15:52 +0000370 // Get Windows SDK installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000371static bool getWindowsSDKDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000372 char windowsSDKInstallDir[256];
373 // Try the Windows registry.
374 bool hasSDKDir = getSystemRegistryString(
375 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
376 "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1);
377 // If we have both vc80 and vc90, pick version we were compiled with.
378 if (hasSDKDir && windowsSDKInstallDir[0]) {
379 path = windowsSDKInstallDir;
380 return(true);
381 }
382 return(false);
383}
384
mike-mc3ec9ae2010-05-06 15:23:52 +0000385void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple) {
Mike Stump43d81762009-10-08 23:29:47 +0000386 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000387 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
388 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000389 llvm::SmallVector<llvm::StringRef, 5> dirs;
390 CIncludeDirs.split(dirs, ":");
391 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
392 i != dirs.end();
393 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000394 AddPath(*i, System, false, false, false);
395 return;
396 }
Mike Stump43d81762009-10-08 23:29:47 +0000397 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000398 switch (os) {
399 case llvm::Triple::Win32:
400 {
Mike Stump620d57a2009-10-12 20:50:45 +0000401 std::string VSDir;
John Thompson75ee3bd2009-11-21 00:15:52 +0000402 std::string WindowsSDKDir;
Mike Stump620d57a2009-10-12 20:50:45 +0000403 if (getVisualStudioDir(VSDir)) {
404 AddPath(VSDir + "\\VC\\include", System, false, false, false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000405 if (getWindowsSDKDir(WindowsSDKDir))
406 AddPath(WindowsSDKDir, System, false, false, false);
407 else
408 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
409 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000410 }
John Thompson9319f022009-11-23 17:49:27 +0000411 else {
412 // Default install paths.
413 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
414 System, false, false, false);
415 AddPath(
416 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
417 System, false, false, false);
418 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
419 System, false, false, false);
420 AddPath(
421 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
422 System, false, false, false);
423 // For some clang developers.
424 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
425 System, false, false, false);
426 AddPath(
427 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
428 System, false, false, false);
429 }
Mike Stump43d81762009-10-08 23:29:47 +0000430 }
431 break;
Chris Lattner86ed3a32010-04-11 19:29:39 +0000432 case llvm::Triple::Haiku:
433 AddPath("/boot/common/include", System, true, false, false);
434 AddPath("/boot/develop/headers/os", System, true, false, false);
435 AddPath("/boot/develop/headers/os/app", System, true, false, false);
436 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
437 AddPath("/boot/develop/headers/os/device", System, true, false, false);
438 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
439 AddPath("/boot/develop/headers/os/game", System, true, false, false);
440 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
441 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
442 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
443 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
444 AddPath("/boot/develop/headers/os/media", System, true, false, false);
445 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
446 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
447 AddPath("/boot/develop/headers/os/net", System, true, false, false);
448 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
449 AddPath("/boot/develop/headers/os/support", System, true, false, false);
450 AddPath("/boot/develop/headers/os/translation",
451 System, true, false, false);
452 AddPath("/boot/develop/headers/os/add-ons/graphics",
453 System, true, false, false);
454 AddPath("/boot/develop/headers/os/add-ons/input_server",
455 System, true, false, false);
456 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
457 System, true, false, false);
458 AddPath("/boot/develop/headers/os/add-ons/tracker",
459 System, true, false, false);
460 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
461 System, true, false, false);
462 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
463 System, true, false, false);
464 AddPath("/boot/develop/headers/os/be_apps/Tracker",
465 System, true, false, false);
466 AddPath("/boot/develop/headers/cpp", System, true, false, false);
467 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
468 System, true, false, false);
469 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
470 AddPath("/boot/develop/headers/bsd", System, true, false, false);
471 AddPath("/boot/develop/headers/glibc", System, true, false, false);
472 AddPath("/boot/develop/headers/posix", System, true, false, false);
473 AddPath("/boot/develop/headers", System, true, false, false);
474 break;
Mike Stump43d81762009-10-08 23:29:47 +0000475 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000476 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000477 AddPath("c:/mingw/include", System, true, false, false);
478 break;
479 default:
Mike Stump43d81762009-10-08 23:29:47 +0000480 break;
481 }
John Thompsond3f88342009-10-13 18:51:32 +0000482
Chris Lattnerfc904d52010-03-06 19:38:10 +0000483 AddPath("/usr/local/include", System, true, false, false);
John Thompsond3f88342009-10-13 18:51:32 +0000484 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000485}
486
Chris Lattner0e3cc052010-05-05 05:28:39 +0000487void InitHeaderSearch::
488AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000489 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000490 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
491 if (CxxIncludeRoot != "") {
492 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
493 if (CxxIncludeArch == "")
494 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
Chris Lattner0e3cc052010-05-05 05:28:39 +0000495 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
496 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000497 else
498 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
Chris Lattner0e3cc052010-05-05 05:28:39 +0000499 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
500 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000501 return;
502 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000503 // FIXME: temporary hack: hard-coded paths.
504 switch (os) {
505 case llvm::Triple::Cygwin:
506 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
507 System, true, false, false);
508 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
509 System, true, false, false);
510 break;
511 case llvm::Triple::MinGW64:
512 // Try gcc 4.4.0
513 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
514 // Try gcc 4.3.0
515 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
516 // Fall through.
517 case llvm::Triple::MinGW32:
518 // Try gcc 4.4.0
519 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
520 // Try gcc 4.3.0
521 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
522 break;
523 case llvm::Triple::Darwin:
524 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000525 "i686-apple-darwin10", "", "x86_64", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000526 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000527 "i686-apple-darwin8", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000528 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000529 case llvm::Triple::DragonFly:
530 AddPath("/usr/include/c++/4.1", System, true, false, false);
531 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000532 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000533 //===------------------------------------------------------------------===//
534 // Debian based distros.
535 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
536 //===------------------------------------------------------------------===//
537 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
538 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
539 // Debian 6.0 "squeeze" -- gcc-4.4.2
540 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
541 "x86_64-linux-gnu", "32", "", triple);
542 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
543 "i486-linux-gnu", "", "64", triple);
544 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
545 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
546 // Debian 5.0 "lenny" -- gcc-4.3.2
547 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
548 "x86_64-linux-gnu", "32", "", triple);
549 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
550 "i486-linux-gnu", "", "64", triple);
551 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
552 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
553 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
554 "x86_64-linux-gnu", "32", "", triple);
555 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
556 "i486-linux-gnu", "", "64", triple);
557 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
558 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
559 "x86_64-linux-gnu", "32", "", triple);
560 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
561 "i486-linux-gnu", "", "64", triple);
562
563 //===------------------------------------------------------------------===//
564 // Redhat based distros.
565 //===------------------------------------------------------------------===//
566 // Fedora 13
567 // Fedora 12
568 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
569 "x86_64-redhat-linux", "32", "", triple);
570 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
571 "i686-redhat-linux","", "", triple);
572 // Fedora 12 (pre-FEB-2010)
573 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
574 "x86_64-redhat-linux", "32", "", triple);
575 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
576 "i686-redhat-linux","", "", triple);
577 // Fedora 11
578 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
579 "x86_64-redhat-linux", "32", "", triple);
580 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
581 "i586-redhat-linux","", "", triple);
582 // Fedora 10
583 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
584 "x86_64-redhat-linux", "32", "", triple);
585 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
586 "i386-redhat-linux","", "", triple);
587 // Fedora 9
588 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
589 "x86_64-redhat-linux", "32", "", triple);
590 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
591 "i386-redhat-linux", "", "", triple);
592 // Fedora 8
593 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
594 "x86_64-redhat-linux", "", "", triple);
595 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
596 "i386-redhat-linux", "", "", triple);
597
598 //===------------------------------------------------------------------===//
599
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000600 // Exherbo (2010-01-25)
601 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000602 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000603 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000604 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000605
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000606 // openSUSE 11.1 32 bit
607 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000608 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000609 // openSUSE 11.1 64 bit
610 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000611 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000612 // openSUSE 11.2
613 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000614 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000615 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000616 "x86_64-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000617 // Arch Linux 2008-06-24
618 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000619 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000620 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000621 "x86_64-unknown-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000622 // Gentoo x86 2009.1 stable
623 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000624 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000625 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000626 // Gentoo x86 2009.0 stable
627 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000628 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000629 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000630 // Gentoo x86 2008.0 stable
631 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000632 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000633 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000634 // Gentoo amd64 stable
635 AddGnuCPlusPlusIncludePaths(
636 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000637 "i686-pc-linux-gnu", "", "", triple);
Eric Christopher70d9d412010-03-03 21:41:50 +0000638
639 // Gentoo amd64 gcc 4.3.2
640 AddGnuCPlusPlusIncludePaths(
641 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
642 "x86_64-pc-linux-gnu", "", "", triple);
643
644 // Gentoo amd64 gcc 4.4.3
645 AddGnuCPlusPlusIncludePaths(
646 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
647 "x86_64-pc-linux-gnu", "32", "", triple);
648
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000649 break;
650 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000651 // FreeBSD 8.0
652 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000653 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000654 break;
655 case llvm::Triple::Solaris:
656 // Solaris - Fall though..
657 case llvm::Triple::AuroraUX:
658 // AuroraUX
659 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000660 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000661 break;
662 default:
663 break;
664 }
665}
666
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000667void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000668 const llvm::Triple &triple,
mike-mc3ec9ae2010-05-06 15:23:52 +0000669 bool UseStandardCXXIncludes) {
670 if (Lang.CPlusPlus && UseStandardCXXIncludes)
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000671 AddDefaultCPlusPlusIncludePaths(triple);
672
mike-mc3ec9ae2010-05-06 15:23:52 +0000673 AddDefaultCIncludePaths(triple);
Daniel Dunbare1665822009-11-07 04:20:39 +0000674
675 // Add the default framework include paths on Darwin.
676 if (triple.getOS() == llvm::Triple::Darwin) {
677 AddPath("/System/Library/Frameworks", System, true, false, true);
678 AddPath("/Library/Frameworks", System, true, false, true);
679 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000680}
681
Nico Weber0fca0222008-08-22 09:25:22 +0000682/// RemoveDuplicates - If there are duplicate directory entries in the specified
683/// search list, remove the later (dead) ones.
684static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
685 bool Verbose) {
686 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
687 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
688 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
689 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000690 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattner43eee072009-02-08 01:00:10 +0000692 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Chris Lattner43eee072009-02-08 01:00:10 +0000694 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000695 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000696 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000697 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000698 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000699 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000700 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000701 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000702 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000703 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000704 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000705 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000706 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000707 }
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Chris Lattner30f05b52009-02-08 00:55:22 +0000709 // If we have a normal #include dir/framework/headermap that is shadowed
710 // later in the chain by a system include location, we actually want to
711 // ignore the user's request and drop the user dir... keeping the system
712 // dir. This is weird, but required to emulate GCC's search path correctly.
713 //
714 // Since dupes of system dirs are rare, just rescan to find the original
715 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000716 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000717 // Find the dir that this is the same of.
718 unsigned FirstDir;
719 for (FirstDir = 0; ; ++FirstDir) {
720 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Chris Lattner43eee072009-02-08 01:00:10 +0000722 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
723
Chris Lattner30f05b52009-02-08 00:55:22 +0000724 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000725 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000726 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Chris Lattner30f05b52009-02-08 00:55:22 +0000728 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000729 if (CurEntry.isNormalDir())
730 isSame = SearchEntry.getDir() == CurEntry.getDir();
731 else if (CurEntry.isFramework())
732 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000733 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000734 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
735 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Chris Lattner30f05b52009-02-08 00:55:22 +0000738 if (isSame)
739 break;
740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Chris Lattner30f05b52009-02-08 00:55:22 +0000742 // If the first dir in the search path is a non-system dir, zap it
743 // instead of the system one.
744 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
745 DirToRemove = FirstDir;
746 }
747
748 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000749 llvm::errs() << "ignoring duplicate directory \""
750 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +0000751 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000752 llvm::errs() << " as it is a non-system directory that duplicates "
753 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000754 }
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Chris Lattner7a739402008-09-26 17:46:45 +0000756 // This is reached if the current entry is a duplicate. Remove the
757 // DirToRemove (usually the current dir).
758 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000759 --i;
760 }
761}
762
763
764void InitHeaderSearch::Realize() {
765 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
766 std::vector<DirectoryLookup> SearchList;
767 SearchList = IncludeGroup[Angled];
768 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
769 IncludeGroup[System].end());
770 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
771 IncludeGroup[After].end());
772 RemoveDuplicates(SearchList, Verbose);
773 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Nico Weber0fca0222008-08-22 09:25:22 +0000775 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000776 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000777 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Nico Weber0fca0222008-08-22 09:25:22 +0000779
780 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
781 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
782 DontSearchCurDir);
783
784 // If verbose, print the list of directories that will be searched.
785 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000786 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000787 unsigned QuotedIdx = IncludeGroup[Quoted].size();
788 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
789 if (i == QuotedIdx)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000790 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000791 const char *Name = SearchList[i].getName();
792 const char *Suffix;
793 if (SearchList[i].isNormalDir())
794 Suffix = "";
795 else if (SearchList[i].isFramework())
796 Suffix = " (framework directory)";
797 else {
798 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
799 Suffix = " (headermap)";
800 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000801 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000802 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000803 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000804 }
805}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000806
Daniel Dunbar5814e652009-11-11 21:44:21 +0000807void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
808 const HeaderSearchOptions &HSOpts,
809 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000810 const llvm::Triple &Triple) {
811 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
812
813 // Add the user defined entries.
814 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
815 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +0000816 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
817 false);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000818 }
819
820 // Add entries from CPATH and friends.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000821 Init.AddDelimitedPaths(HSOpts.EnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000822 if (Lang.CPlusPlus && Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000823 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000824 else if (Lang.CPlusPlus)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000825 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000826 else if (Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000827 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000828 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000829 Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000830
mike-mc3ec9ae2010-05-06 15:23:52 +0000831 if (HSOpts.UseBuiltinIncludes) {
832 // Ignore the sys root, we *always* look for clang headers relative to
833 // supplied path.
834 llvm::sys::Path P(HSOpts.ResourceDir);
835 P.appendComponent("include");
836 Init.AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
837 }
838
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000839 if (HSOpts.UseStandardIncludes)
mike-mc3ec9ae2010-05-06 15:23:52 +0000840 Init.AddDefaultSystemIncludePaths(Lang, Triple,
841 HSOpts.UseStandardCXXIncludes);
842
843 if (HSOpts.UseStandardIncludes)
844 Init.AddDefaultSystemIncludePaths(Lang, Triple,
845 HSOpts.UseStandardCXXIncludes);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000846
847 Init.Realize();
848}