blob: 1244d8bd0ca3d41be6b9042a5fb5f3b98396500e [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
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;
Chandler Carruthc09265a2010-11-15 07:15:26 +000044 llvm::sys::Path IncludeSysroot;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000045
46public:
47
Chandler Carruthc09265a2010-11-15 07:15:26 +000048 InitHeaderSearch(HeaderSearch &HS, bool verbose, llvm::StringRef sysroot)
49 : Headers(HS), Verbose(verbose),
50 IncludeSysroot((sysroot.empty() || sysroot == "/") ?
51 llvm::sys::Path::GetRootDirectory() :
52 llvm::sys::Path(sysroot)) {
53 }
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000054
55 /// AddPath - Add the specified path to the specified group list.
Benjamin Kramere89ba592009-12-08 12:38:20 +000056 void AddPath(const llvm::Twine &Path, IncludeDirGroup Group,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000057 bool isCXXAware, bool isUserSupplied,
58 bool isFramework, bool IgnoreSysRoot = false);
59
mike-ma6087372010-05-05 17:00:31 +000060 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000061 /// libstdc++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000062 void AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
63 llvm::StringRef ArchDir,
64 llvm::StringRef Dir32,
65 llvm::StringRef Dir64,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000066 const llvm::Triple &triple);
67
68 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW
69 /// libstdc++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000070 void AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
71 llvm::StringRef Arch,
72 llvm::StringRef Version);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000073
74 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
75 /// separator. The processing follows that of the CPATH variable for gcc.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +000076 void AddDelimitedPaths(llvm::StringRef String);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000077
78 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-m79bc57c2010-05-16 19:03:52 +000079 void AddDefaultCIncludePaths(const llvm::Triple &triple,
80 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000081
82 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
83 // compiling c++.
84 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
85
86 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
87 /// that e.g. stdio.h is found.
88 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +000089 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +000090 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000091
92 /// Realize - Merges all search path lists into one list and send it to
93 /// HeaderSearch.
94 void Realize();
95};
96
97}
Nico Weber0fca0222008-08-22 09:25:22 +000098
Benjamin Kramere89ba592009-12-08 12:38:20 +000099void InitHeaderSearch::AddPath(const llvm::Twine &Path,
Benjamin Kramer458fb102009-09-05 09:49:39 +0000100 IncludeDirGroup Group, bool isCXXAware,
101 bool isUserSupplied, bool isFramework,
102 bool IgnoreSysRoot) {
Benjamin Kramere89ba592009-12-08 12:38:20 +0000103 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
Nico Weber0fca0222008-08-22 09:25:22 +0000104 FileManager &FM = Headers.getFileMgr();
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000105 const FileSystemOptions &FSOpts = Headers.getFileSystemOpts();
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Nico Weber0fca0222008-08-22 09:25:22 +0000107 // Compute the actual path, taking into consideration -isysroot.
Chandler Carruth5853b0f2010-11-15 00:05:18 +0000108 llvm::SmallString<256> MappedPathStorage;
Chandler Carruthf3721452010-11-15 00:48:13 +0000109 llvm::StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
110 llvm::sys::Path MappedPath(MappedPathStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Nico Weber0fca0222008-08-22 09:25:22 +0000112 // Handle isysroot.
Chandler Carruthc09265a2010-11-15 07:15:26 +0000113 if (Group == System && !IgnoreSysRoot && MappedPath.isAbsolute() &&
Chandler Carruth7c1a1ef2010-11-16 11:30:11 +0000114 IncludeSysroot.isValid() &&
Chandler Carruthc09265a2010-11-15 07:15:26 +0000115 IncludeSysroot != llvm::sys::Path::GetRootDirectory()) {
Chandler Carruth5619ae52010-11-15 09:28:23 +0000116 MappedPathStorage.clear();
Chandler Carruthc09265a2010-11-15 07:15:26 +0000117 MappedPathStr =
118 (IncludeSysroot.str() + Path).toStringRef(MappedPathStorage);
Nico Weber0fca0222008-08-22 09:25:22 +0000119 }
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Nico Weber0fca0222008-08-22 09:25:22 +0000121 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000122 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000123 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000124 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000125 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000126 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000127 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000128 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000129
130
Nico Weber0fca0222008-08-22 09:25:22 +0000131 // If the directory exists, add it.
Chandler Carruthf3721452010-11-15 00:48:13 +0000132 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr, FSOpts)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000133 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
134 isFramework));
135 return;
136 }
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Nico Weber0fca0222008-08-22 09:25:22 +0000138 // Check to see if this is an apple-style headermap (which are not allowed to
139 // be frameworks).
140 if (!isFramework) {
Chandler Carruthf3721452010-11-15 00:48:13 +0000141 if (const FileEntry *FE = FM.getFile(MappedPathStr, FSOpts)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000142 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
143 // It is a headermap, add it to the search path.
144 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
145 return;
146 }
147 }
148 }
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Nico Weber0fca0222008-08-22 09:25:22 +0000150 if (Verbose)
Chandler Carruthf3721452010-11-15 00:48:13 +0000151 llvm::errs() << "ignoring nonexistent directory \""
152 << MappedPathStr << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000153}
154
155
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000156void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) {
157 if (at.empty()) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000158 return;
159
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000160 llvm::StringRef::size_type delim;
161 while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) {
162 if (delim == 0)
Nico Weber0fca0222008-08-22 09:25:22 +0000163 AddPath(".", Angled, false, true, false);
164 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000165 AddPath(at.substr(0, delim), Angled, false, true, false);
166 at = at.substr(delim + 1);
Nico Weber0fca0222008-08-22 09:25:22 +0000167 }
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000168
169 if (at.empty())
Nico Weber0fca0222008-08-22 09:25:22 +0000170 AddPath(".", Angled, false, true, false);
171 else
172 AddPath(at, Angled, false, true, false);
173}
174
Benjamin Kramere89ba592009-12-08 12:38:20 +0000175void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
176 llvm::StringRef ArchDir,
177 llvm::StringRef Dir32,
178 llvm::StringRef Dir64,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000179 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000180 // Add the base dir
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000181 AddPath(Base, System, true, false, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000182
183 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000184 llvm::Triple::ArchType arch = triple.getArch();
185 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000186 if (is64bit)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000187 AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000188 else
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000189 AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000190
191 // Add the backward dir
192 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000193}
Nico Weber0fca0222008-08-22 09:25:22 +0000194
Benjamin Kramere89ba592009-12-08 12:38:20 +0000195void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
196 llvm::StringRef Arch,
197 llvm::StringRef Version) {
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000198 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
199 System, true, false, false);
Chris Lattner8e9006b2010-09-03 16:45:53 +0000200 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
201 System, true, false, false);
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000202 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
203 System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000204}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000205
Mike Stump620d57a2009-10-12 20:50:45 +0000206 // FIXME: This probably should goto to some platform utils place.
207#ifdef _MSC_VER
John Thompson75ee3bd2009-11-21 00:15:52 +0000208
Mike Stump620d57a2009-10-12 20:50:45 +0000209 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000210 // This also supports a means to look for high-versioned keys by use
211 // of a $VERSION placeholder in the key path.
212 // $VERSION in the key path is a placeholder for the version number,
213 // causing the highest value path to be searched for and used.
214 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
215 // There can be additional characters in the component. Only the numberic
216 // characters are compared.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000217static bool getSystemRegistryString(const char *keyPath, const char *valueName,
218 char *value, size_t maxLength) {
Mike Stump43d81762009-10-08 23:29:47 +0000219 HKEY hRootKey = NULL;
220 HKEY hKey = NULL;
221 const char* subKey = NULL;
222 DWORD valueType;
223 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000224 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000225 bool returnValue = false;
226 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
227 hRootKey = HKEY_CLASSES_ROOT;
228 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000229 }
Mike Stump43d81762009-10-08 23:29:47 +0000230 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
231 hRootKey = HKEY_USERS;
232 subKey = keyPath + 11;
233 }
234 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
235 hRootKey = HKEY_LOCAL_MACHINE;
236 subKey = keyPath + 19;
237 }
238 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
239 hRootKey = HKEY_CURRENT_USER;
240 subKey = keyPath + 18;
241 }
242 else
243 return(false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000244 const char *placeHolder = strstr(subKey, "$VERSION");
245 char bestName[256];
246 bestName[0] = '\0';
247 // If we have a $VERSION placeholder, do the highest-version search.
248 if (placeHolder) {
249 const char *keyEnd = placeHolder - 1;
250 const char *nextKey = placeHolder;
251 // Find end of previous key.
252 while ((keyEnd > subKey) && (*keyEnd != '\\'))
253 keyEnd--;
254 // Find end of key containing $VERSION.
255 while (*nextKey && (*nextKey != '\\'))
256 nextKey++;
257 size_t partialKeyLength = keyEnd - subKey;
258 char partialKey[256];
259 if (partialKeyLength > sizeof(partialKey))
260 partialKeyLength = sizeof(partialKey);
261 strncpy(partialKey, subKey, partialKeyLength);
262 partialKey[partialKeyLength] = '\0';
263 HKEY hTopKey = NULL;
264 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
265 if (lResult == ERROR_SUCCESS) {
266 char keyName[256];
267 int bestIndex = -1;
268 double bestValue = 0.0;
269 DWORD index, size = sizeof(keyName) - 1;
Nuno Lopes33cc2432009-12-07 17:18:48 +0000270 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
271 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
272 const char *sp = keyName;
273 while (*sp && !isdigit(*sp))
274 sp++;
275 if (!*sp)
276 continue;
277 const char *ep = sp + 1;
278 while (*ep && (isdigit(*ep) || (*ep == '.')))
279 ep++;
280 char numBuf[32];
281 strncpy(numBuf, sp, sizeof(numBuf) - 1);
282 numBuf[sizeof(numBuf) - 1] = '\0';
283 double value = strtod(numBuf, NULL);
284 if (value > bestValue) {
285 bestIndex = (int)index;
286 bestValue = value;
287 strcpy(bestName, keyName);
288 }
John Thompson75ee3bd2009-11-21 00:15:52 +0000289 size = sizeof(keyName) - 1;
290 }
291 // If we found the highest versioned key, open the key and get the value.
292 if (bestIndex != -1) {
293 // Append rest of key.
294 strncat(bestName, nextKey, sizeof(bestName) - 1);
295 bestName[sizeof(bestName) - 1] = '\0';
296 // Open the chosen key path remainder.
297 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
298 if (lResult == ERROR_SUCCESS) {
299 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
300 (LPBYTE)value, &valueSize);
301 if (lResult == ERROR_SUCCESS)
302 returnValue = true;
303 RegCloseKey(hKey);
304 }
305 }
306 RegCloseKey(hTopKey);
307 }
308 }
309 else {
310 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
311 if (lResult == ERROR_SUCCESS) {
312 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
313 (LPBYTE)value, &valueSize);
314 if (lResult == ERROR_SUCCESS)
315 returnValue = true;
316 RegCloseKey(hKey);
317 }
Mike Stump43d81762009-10-08 23:29:47 +0000318 }
319 return(returnValue);
320}
Mike Stump620d57a2009-10-12 20:50:45 +0000321#else // _MSC_VER
322 // Read registry string.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000323static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
Mike Stump620d57a2009-10-12 20:50:45 +0000324 return(false);
325}
326#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000327
Mike Stump43d81762009-10-08 23:29:47 +0000328 // Get Visual Studio installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000329static bool getVisualStudioDir(std::string &path) {
Michael J. Spencerff58e362010-08-21 21:55:07 +0000330 // First check the environment variables that vsvars32.bat sets.
331 const char* vcinstalldir = getenv("VCINSTALLDIR");
332 if(vcinstalldir) {
333 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
334 if (p)
335 *p = '\0';
336 path = vcinstalldir;
337 return(true);
338 }
339
John Thompson75ee3bd2009-11-21 00:15:52 +0000340 char vsIDEInstallDir[256];
Douglas Gregor80f93d92010-05-18 05:47:04 +0000341 char vsExpressIDEInstallDir[256];
Michael J. Spencerff58e362010-08-21 21:55:07 +0000342 // Then try the windows registry.
John Thompson75ee3bd2009-11-21 00:15:52 +0000343 bool hasVCDir = getSystemRegistryString(
344 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
345 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Douglas Gregor80f93d92010-05-18 05:47:04 +0000346 bool hasVCExpressDir = getSystemRegistryString(
347 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
348 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000349 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000350 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000351 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
352 if (p)
353 *p = '\0';
354 path = vsIDEInstallDir;
355 return(true);
356 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000357 else if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
358 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
359 if (p)
360 *p = '\0';
361 path = vsExpressIDEInstallDir;
362 return(true);
363 }
Mike Stump620d57a2009-10-12 20:50:45 +0000364 else {
365 // Try the environment.
Douglas Gregor80f93d92010-05-18 05:47:04 +0000366 const char* vs100comntools = getenv("VS100COMNTOOLS");
Mike Stump620d57a2009-10-12 20:50:45 +0000367 const char* vs90comntools = getenv("VS90COMNTOOLS");
368 const char* vs80comntools = getenv("VS80COMNTOOLS");
369 const char* vscomntools = NULL;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000370
371 // Try to find the version that we were compiled with
372 if(false) {}
373 #if (_MSC_VER >= 1600) // VC100
374 else if(vs100comntools) {
375 vscomntools = vs100comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000376 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000377 #elif (_MSC_VER == 1500) // VC80
378 else if(vs90comntools) {
379 vscomntools = vs90comntools;
380 }
381 #elif (_MSC_VER == 1400) // VC80
382 else if(vs80comntools) {
383 vscomntools = vs80comntools;
384 }
385 #endif
386 // Otherwise find any version we can
387 else if (vs100comntools)
388 vscomntools = vs100comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000389 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000390 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000391 else if (vs80comntools)
392 vscomntools = vs80comntools;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000393
Mike Stump620d57a2009-10-12 20:50:45 +0000394 if (vscomntools && *vscomntools) {
Dan Gohmancb421fa2010-04-19 16:39:44 +0000395 char *p = const_cast<char *>(strstr(vscomntools, "\\Common7\\Tools"));
Mike Stump620d57a2009-10-12 20:50:45 +0000396 if (p)
397 *p = '\0';
398 path = vscomntools;
399 return(true);
400 }
401 else
402 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000403 }
Mike Stump620d57a2009-10-12 20:50:45 +0000404 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000405}
Mike Stump43d81762009-10-08 23:29:47 +0000406
John Thompson75ee3bd2009-11-21 00:15:52 +0000407 // Get Windows SDK installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000408static bool getWindowsSDKDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000409 char windowsSDKInstallDir[256];
410 // Try the Windows registry.
411 bool hasSDKDir = getSystemRegistryString(
412 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
413 "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1);
414 // If we have both vc80 and vc90, pick version we were compiled with.
415 if (hasSDKDir && windowsSDKInstallDir[0]) {
416 path = windowsSDKInstallDir;
417 return(true);
418 }
419 return(false);
420}
421
mike-m79bc57c2010-05-16 19:03:52 +0000422void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
423 const HeaderSearchOptions &HSOpts) {
Mike Stump43d81762009-10-08 23:29:47 +0000424 // FIXME: temporary hack: hard-coded paths.
mike-m79bc57c2010-05-16 19:03:52 +0000425 AddPath("/usr/local/include", System, true, false, false);
426
427 // Builtin includes use #include_next directives and should be positioned
428 // just prior C include dirs.
429 if (HSOpts.UseBuiltinIncludes) {
430 // Ignore the sys root, we *always* look for clang headers relative to
431 // supplied path.
432 llvm::sys::Path P(HSOpts.ResourceDir);
433 P.appendComponent("include");
434 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
435 }
436
437 // Add dirs specified via 'configure --with-c-include-dirs'.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000438 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
439 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000440 llvm::SmallVector<llvm::StringRef, 5> dirs;
441 CIncludeDirs.split(dirs, ":");
442 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
443 i != dirs.end();
444 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000445 AddPath(*i, System, false, false, false);
446 return;
447 }
Mike Stump43d81762009-10-08 23:29:47 +0000448 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000449 switch (os) {
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000450 case llvm::Triple::Win32: {
451 std::string VSDir;
452 std::string WindowsSDKDir;
453 if (getVisualStudioDir(VSDir)) {
454 AddPath(VSDir + "\\VC\\include", System, false, false, false);
455 if (getWindowsSDKDir(WindowsSDKDir))
456 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
457 else
458 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
459 System, false, false, false);
460 } else {
461 // Default install paths.
462 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
463 System, false, false, false);
464 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
465 System, false, false, false);
466 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000467 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000468 System, false, false, false);
469 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
470 System, false, false, false);
471 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000472 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000473 System, false, false, false);
474 // For some clang developers.
475 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
476 System, false, false, false);
477 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000478 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000479 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000480 }
481 break;
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000482 }
Chris Lattner86ed3a32010-04-11 19:29:39 +0000483 case llvm::Triple::Haiku:
484 AddPath("/boot/common/include", System, true, false, false);
485 AddPath("/boot/develop/headers/os", System, true, false, false);
486 AddPath("/boot/develop/headers/os/app", System, true, false, false);
487 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
488 AddPath("/boot/develop/headers/os/device", System, true, false, false);
489 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
490 AddPath("/boot/develop/headers/os/game", System, true, false, false);
491 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
492 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
493 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
494 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
495 AddPath("/boot/develop/headers/os/media", System, true, false, false);
496 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
497 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
498 AddPath("/boot/develop/headers/os/net", System, true, false, false);
499 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
500 AddPath("/boot/develop/headers/os/support", System, true, false, false);
501 AddPath("/boot/develop/headers/os/translation",
502 System, true, false, false);
503 AddPath("/boot/develop/headers/os/add-ons/graphics",
504 System, true, false, false);
505 AddPath("/boot/develop/headers/os/add-ons/input_server",
506 System, true, false, false);
507 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
508 System, true, false, false);
509 AddPath("/boot/develop/headers/os/add-ons/tracker",
510 System, true, false, false);
511 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
512 System, true, false, false);
513 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
514 System, true, false, false);
515 AddPath("/boot/develop/headers/os/be_apps/Tracker",
516 System, true, false, false);
517 AddPath("/boot/develop/headers/cpp", System, true, false, false);
518 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
519 System, true, false, false);
520 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
521 AddPath("/boot/develop/headers/bsd", System, true, false, false);
522 AddPath("/boot/develop/headers/glibc", System, true, false, false);
523 AddPath("/boot/develop/headers/posix", System, true, false, false);
524 AddPath("/boot/develop/headers", System, true, false, false);
Eli Friedmana7e68452010-08-22 01:00:03 +0000525 break;
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000526 case llvm::Triple::Cygwin:
527 AddPath("/usr/include/w32api", System, true, false, false);
528 break;
Mike Stump43d81762009-10-08 23:29:47 +0000529 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000530 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000531 AddPath("c:/mingw/include", System, true, false, false);
532 break;
533 default:
Mike Stump43d81762009-10-08 23:29:47 +0000534 break;
535 }
John Thompsond3f88342009-10-13 18:51:32 +0000536
John Thompsond3f88342009-10-13 18:51:32 +0000537 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000538}
539
Chris Lattner0e3cc052010-05-05 05:28:39 +0000540void InitHeaderSearch::
541AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000542 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000543 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
544 if (CxxIncludeRoot != "") {
545 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
546 if (CxxIncludeArch == "")
547 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
Chris Lattner0e3cc052010-05-05 05:28:39 +0000548 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
549 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000550 else
551 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
Chris Lattner0e3cc052010-05-05 05:28:39 +0000552 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
553 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000554 return;
555 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000556 // FIXME: temporary hack: hard-coded paths.
557 switch (os) {
558 case llvm::Triple::Cygwin:
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000559 // Cygwin-1.7
560 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
561 // g++-4 / Cygwin-1.5
562 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
563 // FIXME: Do we support g++-3.4.4?
564 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000565 break;
566 case llvm::Triple::MinGW64:
Chris Lattner6a081402010-09-01 15:51:58 +0000567 // Try gcc 4.5.0
568 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.5.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000569 // Try gcc 4.4.0
570 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
571 // Try gcc 4.3.0
572 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
573 // Fall through.
574 case llvm::Triple::MinGW32:
Chris Lattner6a081402010-09-01 15:51:58 +0000575 // Try gcc 4.5.0
576 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000577 // Try gcc 4.4.0
578 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
579 // Try gcc 4.3.0
580 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
581 break;
582 case llvm::Triple::Darwin:
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000583 switch (triple.getArch()) {
584 default: break;
585
Douglas Gregor582c3012010-05-29 01:15:12 +0000586 case llvm::Triple::ppc:
587 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000588 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
589 "powerpc-apple-darwin10", "", "ppc64",
590 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000591 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
592 "powerpc-apple-darwin10", "", "ppc64",
593 triple);
594 break;
595
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000596 case llvm::Triple::x86:
597 case llvm::Triple::x86_64:
598 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
599 "i686-apple-darwin10", "", "x86_64", triple);
600 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
601 "i686-apple-darwin8", "", "", triple);
602 break;
603
604 case llvm::Triple::arm:
605 case llvm::Triple::thumb:
606 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
607 "arm-apple-darwin10", "v7", "", triple);
608 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
609 "arm-apple-darwin10", "v6", "", triple);
610 break;
611 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000612 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000613 case llvm::Triple::DragonFly:
614 AddPath("/usr/include/c++/4.1", System, true, false, false);
615 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000616 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000617 //===------------------------------------------------------------------===//
618 // Debian based distros.
619 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
620 //===------------------------------------------------------------------===//
621 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
622 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
623 // Debian 6.0 "squeeze" -- gcc-4.4.2
624 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
625 "x86_64-linux-gnu", "32", "", triple);
626 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
627 "i486-linux-gnu", "", "64", triple);
628 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
629 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
630 // Debian 5.0 "lenny" -- gcc-4.3.2
631 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
632 "x86_64-linux-gnu", "32", "", triple);
633 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
634 "i486-linux-gnu", "", "64", triple);
Rafael Espindolae7d6c2c2010-06-04 14:28:10 +0000635 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
636 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000637 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
638 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
639 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
640 "x86_64-linux-gnu", "32", "", triple);
641 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
642 "i486-linux-gnu", "", "64", triple);
643 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
644 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
645 "x86_64-linux-gnu", "32", "", triple);
646 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
647 "i486-linux-gnu", "", "64", triple);
648
649 //===------------------------------------------------------------------===//
650 // Redhat based distros.
651 //===------------------------------------------------------------------===//
Rafael Espindola6638b3a2010-11-02 18:39:34 +0000652 // Fedora 14
653 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
654 "x86_64-redhat-linux", "32", "", triple);
655 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
656 "i686-redhat-linux", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000657 // Fedora 13
Chris Lattner4336e192010-05-29 01:01:38 +0000658 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
659 "x86_64-redhat-linux", "32", "", triple);
660 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
661 "i686-redhat-linux","", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000662 // Fedora 12
663 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
664 "x86_64-redhat-linux", "32", "", triple);
665 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
666 "i686-redhat-linux","", "", triple);
667 // Fedora 12 (pre-FEB-2010)
668 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
669 "x86_64-redhat-linux", "32", "", triple);
670 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
671 "i686-redhat-linux","", "", triple);
672 // Fedora 11
673 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
674 "x86_64-redhat-linux", "32", "", triple);
675 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
676 "i586-redhat-linux","", "", triple);
677 // Fedora 10
678 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
679 "x86_64-redhat-linux", "32", "", triple);
680 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
681 "i386-redhat-linux","", "", triple);
682 // Fedora 9
683 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
684 "x86_64-redhat-linux", "32", "", triple);
685 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
686 "i386-redhat-linux", "", "", triple);
687 // Fedora 8
688 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
689 "x86_64-redhat-linux", "", "", triple);
690 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
691 "i386-redhat-linux", "", "", triple);
692
693 //===------------------------------------------------------------------===//
694
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000695 // Exherbo (2010-01-25)
696 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000697 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000698 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000699 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000700
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000701 // openSUSE 11.1 32 bit
702 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000703 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000704 // openSUSE 11.1 64 bit
705 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000706 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000707 // openSUSE 11.2
708 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000709 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000710 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000711 "x86_64-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000712 // Arch Linux 2008-06-24
713 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000714 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000715 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000716 "x86_64-unknown-linux-gnu", "", "", triple);
Nuno Lopes0d155a52010-09-11 17:51:45 +0000717 // Gentoo x86 2010.0 stable
718 AddGnuCPlusPlusIncludePaths(
719 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
720 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000721 // Gentoo x86 2009.1 stable
722 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000723 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000724 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000725 // Gentoo x86 2009.0 stable
726 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000727 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000728 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000729 // Gentoo x86 2008.0 stable
730 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000731 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000732 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000733 // Gentoo amd64 stable
734 AddGnuCPlusPlusIncludePaths(
735 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000736 "i686-pc-linux-gnu", "", "", triple);
Eric Christopher70d9d412010-03-03 21:41:50 +0000737
738 // Gentoo amd64 gcc 4.3.2
739 AddGnuCPlusPlusIncludePaths(
740 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
741 "x86_64-pc-linux-gnu", "", "", triple);
742
743 // Gentoo amd64 gcc 4.4.3
744 AddGnuCPlusPlusIncludePaths(
745 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
746 "x86_64-pc-linux-gnu", "32", "", triple);
Nick Lewycky66935342010-07-24 21:33:13 +0000747
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000748 // Gentoo amd64 gcc 4.4.4
749 AddGnuCPlusPlusIncludePaths(
750 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
751 "x86_64-pc-linux-gnu", "32", "", triple);
752
Nick Lewycky66935342010-07-24 21:33:13 +0000753 // Gentoo amd64 llvm-gcc trunk
754 AddGnuCPlusPlusIncludePaths(
755 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
756 "x86_64-pc-linux-gnu", "", "", triple);
Eric Christopher70d9d412010-03-03 21:41:50 +0000757
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000758 break;
759 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000760 // FreeBSD 8.0
761 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000762 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000763 break;
Anton Korobeynikovab079412010-08-31 22:39:50 +0000764 case llvm::Triple::NetBSD:
765 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
766 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000767 case llvm::Triple::OpenBSD: {
768 std::string t = triple.getTriple();
769 if (t.substr(0, 6) == "x86_64")
770 t.replace(0, 6, "amd64");
771 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
772 t, "", "", triple);
773 break;
774 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000775 case llvm::Triple::Minix:
776 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
777 "", "", "", triple);
778 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000779 case llvm::Triple::Solaris:
780 // Solaris - Fall though..
781 case llvm::Triple::AuroraUX:
782 // AuroraUX
783 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000784 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000785 break;
786 default:
787 break;
788 }
789}
790
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000791void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000792 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000793 const HeaderSearchOptions &HSOpts) {
Daniel Dunbar80c26f42010-09-09 17:38:22 +0000794 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes) {
795 if (!HSOpts.CXXSystemIncludes.empty()) {
796 for (unsigned i = 0, e = HSOpts.CXXSystemIncludes.size(); i != e; ++i)
797 AddPath(HSOpts.CXXSystemIncludes[i], System, true, false, false);
798 } else
799 AddDefaultCPlusPlusIncludePaths(triple);
800 }
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000801
mike-m79bc57c2010-05-16 19:03:52 +0000802 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000803
804 // Add the default framework include paths on Darwin.
805 if (triple.getOS() == llvm::Triple::Darwin) {
806 AddPath("/System/Library/Frameworks", System, true, false, true);
807 AddPath("/Library/Frameworks", System, true, false, true);
808 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000809}
810
Nico Weber0fca0222008-08-22 09:25:22 +0000811/// RemoveDuplicates - If there are duplicate directory entries in the specified
812/// search list, remove the later (dead) ones.
813static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
814 bool Verbose) {
815 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
816 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
817 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
818 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000819 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Chris Lattner43eee072009-02-08 01:00:10 +0000821 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Chris Lattner43eee072009-02-08 01:00:10 +0000823 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000824 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000825 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000826 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000827 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000828 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000829 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000830 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000831 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000832 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000833 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000834 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000835 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000836 }
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Chris Lattner30f05b52009-02-08 00:55:22 +0000838 // If we have a normal #include dir/framework/headermap that is shadowed
839 // later in the chain by a system include location, we actually want to
840 // ignore the user's request and drop the user dir... keeping the system
841 // dir. This is weird, but required to emulate GCC's search path correctly.
842 //
843 // Since dupes of system dirs are rare, just rescan to find the original
844 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000845 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000846 // Find the dir that this is the same of.
847 unsigned FirstDir;
848 for (FirstDir = 0; ; ++FirstDir) {
849 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Chris Lattner43eee072009-02-08 01:00:10 +0000851 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
852
Chris Lattner30f05b52009-02-08 00:55:22 +0000853 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000854 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000855 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Chris Lattner30f05b52009-02-08 00:55:22 +0000857 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000858 if (CurEntry.isNormalDir())
859 isSame = SearchEntry.getDir() == CurEntry.getDir();
860 else if (CurEntry.isFramework())
861 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000862 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000863 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
864 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000865 }
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Chris Lattner30f05b52009-02-08 00:55:22 +0000867 if (isSame)
868 break;
869 }
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Chris Lattner30f05b52009-02-08 00:55:22 +0000871 // If the first dir in the search path is a non-system dir, zap it
872 // instead of the system one.
873 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
874 DirToRemove = FirstDir;
875 }
876
877 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000878 llvm::errs() << "ignoring duplicate directory \""
879 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +0000880 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000881 llvm::errs() << " as it is a non-system directory that duplicates "
882 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Chris Lattner7a739402008-09-26 17:46:45 +0000885 // This is reached if the current entry is a duplicate. Remove the
886 // DirToRemove (usually the current dir).
887 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000888 --i;
889 }
890}
891
892
893void InitHeaderSearch::Realize() {
894 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
895 std::vector<DirectoryLookup> SearchList;
896 SearchList = IncludeGroup[Angled];
897 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
898 IncludeGroup[System].end());
899 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
900 IncludeGroup[After].end());
901 RemoveDuplicates(SearchList, Verbose);
902 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Nico Weber0fca0222008-08-22 09:25:22 +0000904 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000905 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000906 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Nico Weber0fca0222008-08-22 09:25:22 +0000908
909 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
910 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
911 DontSearchCurDir);
912
913 // If verbose, print the list of directories that will be searched.
914 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000915 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000916 unsigned QuotedIdx = IncludeGroup[Quoted].size();
917 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
918 if (i == QuotedIdx)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000919 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000920 const char *Name = SearchList[i].getName();
921 const char *Suffix;
922 if (SearchList[i].isNormalDir())
923 Suffix = "";
924 else if (SearchList[i].isFramework())
925 Suffix = " (framework directory)";
926 else {
927 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
928 Suffix = " (headermap)";
929 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000930 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000931 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000932 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000933 }
934}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000935
Daniel Dunbar5814e652009-11-11 21:44:21 +0000936void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
937 const HeaderSearchOptions &HSOpts,
938 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000939 const llvm::Triple &Triple) {
940 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
941
942 // Add the user defined entries.
943 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
944 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +0000945 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
Chris Lattner23637be2010-08-24 22:27:37 +0000946 !E.IsSysRootRelative);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000947 }
948
949 // Add entries from CPATH and friends.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000950 Init.AddDelimitedPaths(HSOpts.EnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000951 if (Lang.CPlusPlus && Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000952 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000953 else if (Lang.CPlusPlus)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000954 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000955 else if (Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000956 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000957 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000958 Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000959
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000960 if (HSOpts.UseStandardIncludes)
mike-m79bc57c2010-05-16 19:03:52 +0000961 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000962
963 Init.Realize();
964}