blob: 438556e8da6708e08a31250a9d78d912e3ffb626 [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"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000026#include "llvm/Support/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();
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Nico Weber0fca0222008-08-22 09:25:22 +0000106 // Compute the actual path, taking into consideration -isysroot.
Chandler Carruth5853b0f2010-11-15 00:05:18 +0000107 llvm::SmallString<256> MappedPathStorage;
Chandler Carruthf3721452010-11-15 00:48:13 +0000108 llvm::StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
109 llvm::sys::Path MappedPath(MappedPathStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Nico Weber0fca0222008-08-22 09:25:22 +0000111 // Handle isysroot.
Chandler Carruthc09265a2010-11-15 07:15:26 +0000112 if (Group == System && !IgnoreSysRoot && MappedPath.isAbsolute() &&
Chandler Carruthc09265a2010-11-15 07:15:26 +0000113 IncludeSysroot != llvm::sys::Path::GetRootDirectory()) {
Chandler Carruth5619ae52010-11-15 09:28:23 +0000114 MappedPathStorage.clear();
Chandler Carruthc09265a2010-11-15 07:15:26 +0000115 MappedPathStr =
116 (IncludeSysroot.str() + Path).toStringRef(MappedPathStorage);
Nico Weber0fca0222008-08-22 09:25:22 +0000117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Nico Weber0fca0222008-08-22 09:25:22 +0000119 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000120 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000121 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000122 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000123 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000124 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000125 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000126 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
128
Nico Weber0fca0222008-08-22 09:25:22 +0000129 // If the directory exists, add it.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000130 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000131 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
132 isFramework));
133 return;
134 }
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Nico Weber0fca0222008-08-22 09:25:22 +0000136 // Check to see if this is an apple-style headermap (which are not allowed to
137 // be frameworks).
138 if (!isFramework) {
Chris Lattner39b49bc2010-11-23 08:35:12 +0000139 if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000140 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
141 // It is a headermap, add it to the search path.
142 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
143 return;
144 }
145 }
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Nico Weber0fca0222008-08-22 09:25:22 +0000148 if (Verbose)
Chandler Carruthf3721452010-11-15 00:48:13 +0000149 llvm::errs() << "ignoring nonexistent directory \""
150 << MappedPathStr << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000151}
152
153
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000154void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) {
155 if (at.empty()) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000156 return;
157
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000158 llvm::StringRef::size_type delim;
159 while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) {
160 if (delim == 0)
Nico Weber0fca0222008-08-22 09:25:22 +0000161 AddPath(".", Angled, false, true, false);
162 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000163 AddPath(at.substr(0, delim), Angled, false, true, false);
164 at = at.substr(delim + 1);
Nico Weber0fca0222008-08-22 09:25:22 +0000165 }
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000166
167 if (at.empty())
Nico Weber0fca0222008-08-22 09:25:22 +0000168 AddPath(".", Angled, false, true, false);
169 else
170 AddPath(at, Angled, false, true, false);
171}
172
Benjamin Kramere89ba592009-12-08 12:38:20 +0000173void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
174 llvm::StringRef ArchDir,
175 llvm::StringRef Dir32,
176 llvm::StringRef Dir64,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000177 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000178 // Add the base dir
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000179 AddPath(Base, System, true, false, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000180
181 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000182 llvm::Triple::ArchType arch = triple.getArch();
183 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000184 if (is64bit)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000185 AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000186 else
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000187 AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000188
189 // Add the backward dir
190 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000191}
Nico Weber0fca0222008-08-22 09:25:22 +0000192
Benjamin Kramere89ba592009-12-08 12:38:20 +0000193void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
194 llvm::StringRef Arch,
195 llvm::StringRef Version) {
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000196 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
197 System, true, false, false);
Chris Lattner8e9006b2010-09-03 16:45:53 +0000198 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
199 System, true, false, false);
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000200 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
201 System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000202}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000203
Mike Stump620d57a2009-10-12 20:50:45 +0000204 // FIXME: This probably should goto to some platform utils place.
205#ifdef _MSC_VER
John Thompson75ee3bd2009-11-21 00:15:52 +0000206
Mike Stump620d57a2009-10-12 20:50:45 +0000207 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000208 // This also supports a means to look for high-versioned keys by use
209 // of a $VERSION placeholder in the key path.
210 // $VERSION in the key path is a placeholder for the version number,
211 // causing the highest value path to be searched for and used.
212 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
213 // There can be additional characters in the component. Only the numberic
214 // characters are compared.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000215static bool getSystemRegistryString(const char *keyPath, const char *valueName,
216 char *value, size_t maxLength) {
Mike Stump43d81762009-10-08 23:29:47 +0000217 HKEY hRootKey = NULL;
218 HKEY hKey = NULL;
219 const char* subKey = NULL;
220 DWORD valueType;
221 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000222 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000223 bool returnValue = false;
224 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
225 hRootKey = HKEY_CLASSES_ROOT;
226 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000227 }
Mike Stump43d81762009-10-08 23:29:47 +0000228 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
229 hRootKey = HKEY_USERS;
230 subKey = keyPath + 11;
231 }
232 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
233 hRootKey = HKEY_LOCAL_MACHINE;
234 subKey = keyPath + 19;
235 }
236 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
237 hRootKey = HKEY_CURRENT_USER;
238 subKey = keyPath + 18;
239 }
240 else
241 return(false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000242 const char *placeHolder = strstr(subKey, "$VERSION");
243 char bestName[256];
244 bestName[0] = '\0';
245 // If we have a $VERSION placeholder, do the highest-version search.
246 if (placeHolder) {
247 const char *keyEnd = placeHolder - 1;
248 const char *nextKey = placeHolder;
249 // Find end of previous key.
250 while ((keyEnd > subKey) && (*keyEnd != '\\'))
251 keyEnd--;
252 // Find end of key containing $VERSION.
253 while (*nextKey && (*nextKey != '\\'))
254 nextKey++;
255 size_t partialKeyLength = keyEnd - subKey;
256 char partialKey[256];
257 if (partialKeyLength > sizeof(partialKey))
258 partialKeyLength = sizeof(partialKey);
259 strncpy(partialKey, subKey, partialKeyLength);
260 partialKey[partialKeyLength] = '\0';
261 HKEY hTopKey = NULL;
262 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
263 if (lResult == ERROR_SUCCESS) {
264 char keyName[256];
265 int bestIndex = -1;
266 double bestValue = 0.0;
267 DWORD index, size = sizeof(keyName) - 1;
Nuno Lopes33cc2432009-12-07 17:18:48 +0000268 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
269 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
270 const char *sp = keyName;
271 while (*sp && !isdigit(*sp))
272 sp++;
273 if (!*sp)
274 continue;
275 const char *ep = sp + 1;
276 while (*ep && (isdigit(*ep) || (*ep == '.')))
277 ep++;
278 char numBuf[32];
279 strncpy(numBuf, sp, sizeof(numBuf) - 1);
280 numBuf[sizeof(numBuf) - 1] = '\0';
281 double value = strtod(numBuf, NULL);
282 if (value > bestValue) {
283 bestIndex = (int)index;
284 bestValue = value;
285 strcpy(bestName, keyName);
286 }
John Thompson75ee3bd2009-11-21 00:15:52 +0000287 size = sizeof(keyName) - 1;
288 }
289 // If we found the highest versioned key, open the key and get the value.
290 if (bestIndex != -1) {
291 // Append rest of key.
292 strncat(bestName, nextKey, sizeof(bestName) - 1);
293 bestName[sizeof(bestName) - 1] = '\0';
294 // Open the chosen key path remainder.
295 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
296 if (lResult == ERROR_SUCCESS) {
297 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
298 (LPBYTE)value, &valueSize);
299 if (lResult == ERROR_SUCCESS)
300 returnValue = true;
301 RegCloseKey(hKey);
302 }
303 }
304 RegCloseKey(hTopKey);
305 }
306 }
307 else {
308 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
309 if (lResult == ERROR_SUCCESS) {
310 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
311 (LPBYTE)value, &valueSize);
312 if (lResult == ERROR_SUCCESS)
313 returnValue = true;
314 RegCloseKey(hKey);
315 }
Mike Stump43d81762009-10-08 23:29:47 +0000316 }
317 return(returnValue);
318}
Mike Stump620d57a2009-10-12 20:50:45 +0000319#else // _MSC_VER
320 // Read registry string.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000321static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
Mike Stump620d57a2009-10-12 20:50:45 +0000322 return(false);
323}
324#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000325
Mike Stump43d81762009-10-08 23:29:47 +0000326 // Get Visual Studio installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000327static bool getVisualStudioDir(std::string &path) {
Michael J. Spencerff58e362010-08-21 21:55:07 +0000328 // First check the environment variables that vsvars32.bat sets.
329 const char* vcinstalldir = getenv("VCINSTALLDIR");
330 if(vcinstalldir) {
331 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
332 if (p)
333 *p = '\0';
334 path = vcinstalldir;
335 return(true);
336 }
337
John Thompson75ee3bd2009-11-21 00:15:52 +0000338 char vsIDEInstallDir[256];
Douglas Gregor80f93d92010-05-18 05:47:04 +0000339 char vsExpressIDEInstallDir[256];
Michael J. Spencerff58e362010-08-21 21:55:07 +0000340 // Then try the windows registry.
John Thompson75ee3bd2009-11-21 00:15:52 +0000341 bool hasVCDir = getSystemRegistryString(
342 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
343 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Douglas Gregor80f93d92010-05-18 05:47:04 +0000344 bool hasVCExpressDir = getSystemRegistryString(
345 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
346 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000347 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000348 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000349 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
350 if (p)
351 *p = '\0';
352 path = vsIDEInstallDir;
353 return(true);
354 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000355 else if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
356 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
357 if (p)
358 *p = '\0';
359 path = vsExpressIDEInstallDir;
360 return(true);
361 }
Mike Stump620d57a2009-10-12 20:50:45 +0000362 else {
363 // Try the environment.
Douglas Gregor80f93d92010-05-18 05:47:04 +0000364 const char* vs100comntools = getenv("VS100COMNTOOLS");
Mike Stump620d57a2009-10-12 20:50:45 +0000365 const char* vs90comntools = getenv("VS90COMNTOOLS");
366 const char* vs80comntools = getenv("VS80COMNTOOLS");
367 const char* vscomntools = NULL;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000368
369 // Try to find the version that we were compiled with
370 if(false) {}
371 #if (_MSC_VER >= 1600) // VC100
372 else if(vs100comntools) {
373 vscomntools = vs100comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000374 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000375 #elif (_MSC_VER == 1500) // VC80
376 else if(vs90comntools) {
377 vscomntools = vs90comntools;
378 }
379 #elif (_MSC_VER == 1400) // VC80
380 else if(vs80comntools) {
381 vscomntools = vs80comntools;
382 }
383 #endif
384 // Otherwise find any version we can
385 else if (vs100comntools)
386 vscomntools = vs100comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000387 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000388 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000389 else if (vs80comntools)
390 vscomntools = vs80comntools;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000391
Mike Stump620d57a2009-10-12 20:50:45 +0000392 if (vscomntools && *vscomntools) {
Dan Gohmancb421fa2010-04-19 16:39:44 +0000393 char *p = const_cast<char *>(strstr(vscomntools, "\\Common7\\Tools"));
Mike Stump620d57a2009-10-12 20:50:45 +0000394 if (p)
395 *p = '\0';
396 path = vscomntools;
397 return(true);
398 }
399 else
400 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000401 }
Mike Stump620d57a2009-10-12 20:50:45 +0000402 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000403}
Mike Stump43d81762009-10-08 23:29:47 +0000404
John Thompson75ee3bd2009-11-21 00:15:52 +0000405 // Get Windows SDK installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000406static bool getWindowsSDKDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000407 char windowsSDKInstallDir[256];
408 // Try the Windows registry.
409 bool hasSDKDir = getSystemRegistryString(
410 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
411 "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1);
412 // If we have both vc80 and vc90, pick version we were compiled with.
413 if (hasSDKDir && windowsSDKInstallDir[0]) {
414 path = windowsSDKInstallDir;
415 return(true);
416 }
417 return(false);
418}
419
mike-m79bc57c2010-05-16 19:03:52 +0000420void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
421 const HeaderSearchOptions &HSOpts) {
Mike Stump43d81762009-10-08 23:29:47 +0000422 // FIXME: temporary hack: hard-coded paths.
mike-m79bc57c2010-05-16 19:03:52 +0000423 AddPath("/usr/local/include", System, true, false, false);
424
425 // Builtin includes use #include_next directives and should be positioned
426 // just prior C include dirs.
427 if (HSOpts.UseBuiltinIncludes) {
428 // Ignore the sys root, we *always* look for clang headers relative to
429 // supplied path.
430 llvm::sys::Path P(HSOpts.ResourceDir);
431 P.appendComponent("include");
432 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
433 }
434
435 // Add dirs specified via 'configure --with-c-include-dirs'.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000436 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
437 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000438 llvm::SmallVector<llvm::StringRef, 5> dirs;
439 CIncludeDirs.split(dirs, ":");
440 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
441 i != dirs.end();
442 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000443 AddPath(*i, System, false, false, false);
444 return;
445 }
Mike Stump43d81762009-10-08 23:29:47 +0000446 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000447 switch (os) {
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000448 case llvm::Triple::Win32: {
449 std::string VSDir;
450 std::string WindowsSDKDir;
451 if (getVisualStudioDir(VSDir)) {
452 AddPath(VSDir + "\\VC\\include", System, false, false, false);
453 if (getWindowsSDKDir(WindowsSDKDir))
454 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
455 else
456 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
457 System, false, false, false);
458 } else {
459 // Default install paths.
460 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
461 System, false, false, false);
462 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
463 System, false, false, false);
464 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000465 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000466 System, false, false, false);
467 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
468 System, false, false, false);
469 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000470 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000471 System, false, false, false);
472 // For some clang developers.
473 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
474 System, false, false, false);
475 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000476 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000477 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000478 }
479 break;
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000480 }
Chris Lattner86ed3a32010-04-11 19:29:39 +0000481 case llvm::Triple::Haiku:
482 AddPath("/boot/common/include", System, true, false, false);
483 AddPath("/boot/develop/headers/os", System, true, false, false);
484 AddPath("/boot/develop/headers/os/app", System, true, false, false);
485 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
486 AddPath("/boot/develop/headers/os/device", System, true, false, false);
487 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
488 AddPath("/boot/develop/headers/os/game", System, true, false, false);
489 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
490 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
491 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
492 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
493 AddPath("/boot/develop/headers/os/media", System, true, false, false);
494 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
495 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
496 AddPath("/boot/develop/headers/os/net", System, true, false, false);
497 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
498 AddPath("/boot/develop/headers/os/support", System, true, false, false);
499 AddPath("/boot/develop/headers/os/translation",
500 System, true, false, false);
501 AddPath("/boot/develop/headers/os/add-ons/graphics",
502 System, true, false, false);
503 AddPath("/boot/develop/headers/os/add-ons/input_server",
504 System, true, false, false);
505 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
506 System, true, false, false);
507 AddPath("/boot/develop/headers/os/add-ons/tracker",
508 System, true, false, false);
509 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
510 System, true, false, false);
511 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
512 System, true, false, false);
513 AddPath("/boot/develop/headers/os/be_apps/Tracker",
514 System, true, false, false);
515 AddPath("/boot/develop/headers/cpp", System, true, false, false);
516 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
517 System, true, false, false);
518 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
519 AddPath("/boot/develop/headers/bsd", System, true, false, false);
520 AddPath("/boot/develop/headers/glibc", System, true, false, false);
521 AddPath("/boot/develop/headers/posix", System, true, false, false);
522 AddPath("/boot/develop/headers", System, true, false, false);
Eli Friedmana7e68452010-08-22 01:00:03 +0000523 break;
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000524 case llvm::Triple::Cygwin:
525 AddPath("/usr/include/w32api", System, true, false, false);
526 break;
Mike Stump43d81762009-10-08 23:29:47 +0000527 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000528 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000529 AddPath("c:/mingw/include", System, true, false, false);
530 break;
531 default:
Mike Stump43d81762009-10-08 23:29:47 +0000532 break;
533 }
John Thompsond3f88342009-10-13 18:51:32 +0000534
John Thompsond3f88342009-10-13 18:51:32 +0000535 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000536}
537
Chris Lattner0e3cc052010-05-05 05:28:39 +0000538void InitHeaderSearch::
539AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000540 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000541 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
542 if (CxxIncludeRoot != "") {
543 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
544 if (CxxIncludeArch == "")
545 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
Chris Lattner0e3cc052010-05-05 05:28:39 +0000546 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
547 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000548 else
549 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
Chris Lattner0e3cc052010-05-05 05:28:39 +0000550 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
551 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000552 return;
553 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000554 // FIXME: temporary hack: hard-coded paths.
555 switch (os) {
556 case llvm::Triple::Cygwin:
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000557 // Cygwin-1.7
558 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
559 // g++-4 / Cygwin-1.5
560 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
561 // FIXME: Do we support g++-3.4.4?
562 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000563 break;
564 case llvm::Triple::MinGW64:
Chris Lattner6a081402010-09-01 15:51:58 +0000565 // Try gcc 4.5.0
566 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.5.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000567 // Try gcc 4.4.0
568 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
569 // Try gcc 4.3.0
570 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
571 // Fall through.
572 case llvm::Triple::MinGW32:
Chris Lattner6a081402010-09-01 15:51:58 +0000573 // Try gcc 4.5.0
574 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000575 // Try gcc 4.4.0
576 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
577 // Try gcc 4.3.0
578 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
579 break;
580 case llvm::Triple::Darwin:
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000581 switch (triple.getArch()) {
582 default: break;
583
Douglas Gregor582c3012010-05-29 01:15:12 +0000584 case llvm::Triple::ppc:
585 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000586 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
587 "powerpc-apple-darwin10", "", "ppc64",
588 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000589 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
590 "powerpc-apple-darwin10", "", "ppc64",
591 triple);
592 break;
593
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000594 case llvm::Triple::x86:
595 case llvm::Triple::x86_64:
596 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
597 "i686-apple-darwin10", "", "x86_64", triple);
598 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
599 "i686-apple-darwin8", "", "", triple);
600 break;
601
602 case llvm::Triple::arm:
603 case llvm::Triple::thumb:
604 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
605 "arm-apple-darwin10", "v7", "", triple);
606 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
607 "arm-apple-darwin10", "v6", "", triple);
608 break;
609 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000610 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000611 case llvm::Triple::DragonFly:
612 AddPath("/usr/include/c++/4.1", System, true, false, false);
613 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000614 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000615 //===------------------------------------------------------------------===//
616 // Debian based distros.
617 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
618 //===------------------------------------------------------------------===//
619 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
620 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
621 // Debian 6.0 "squeeze" -- gcc-4.4.2
622 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
623 "x86_64-linux-gnu", "32", "", triple);
624 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
625 "i486-linux-gnu", "", "64", triple);
626 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
627 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
628 // Debian 5.0 "lenny" -- gcc-4.3.2
629 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
630 "x86_64-linux-gnu", "32", "", triple);
631 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
632 "i486-linux-gnu", "", "64", triple);
Rafael Espindolae7d6c2c2010-06-04 14:28:10 +0000633 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
634 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000635 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
636 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
637 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
638 "x86_64-linux-gnu", "32", "", triple);
639 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
640 "i486-linux-gnu", "", "64", triple);
641 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
642 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
643 "x86_64-linux-gnu", "32", "", triple);
644 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
645 "i486-linux-gnu", "", "64", triple);
646
647 //===------------------------------------------------------------------===//
648 // Redhat based distros.
649 //===------------------------------------------------------------------===//
Rafael Espindola6638b3a2010-11-02 18:39:34 +0000650 // Fedora 14
651 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
652 "x86_64-redhat-linux", "32", "", triple);
653 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
654 "i686-redhat-linux", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000655 // Fedora 13
Chris Lattner4336e192010-05-29 01:01:38 +0000656 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
657 "x86_64-redhat-linux", "32", "", triple);
658 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
659 "i686-redhat-linux","", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000660 // Fedora 12
661 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
662 "x86_64-redhat-linux", "32", "", triple);
663 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
664 "i686-redhat-linux","", "", triple);
665 // Fedora 12 (pre-FEB-2010)
666 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
667 "x86_64-redhat-linux", "32", "", triple);
668 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
669 "i686-redhat-linux","", "", triple);
670 // Fedora 11
671 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
672 "x86_64-redhat-linux", "32", "", triple);
673 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
674 "i586-redhat-linux","", "", triple);
675 // Fedora 10
676 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
677 "x86_64-redhat-linux", "32", "", triple);
678 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
679 "i386-redhat-linux","", "", triple);
680 // Fedora 9
681 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
682 "x86_64-redhat-linux", "32", "", triple);
683 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
684 "i386-redhat-linux", "", "", triple);
685 // Fedora 8
686 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
687 "x86_64-redhat-linux", "", "", triple);
688 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
689 "i386-redhat-linux", "", "", triple);
690
691 //===------------------------------------------------------------------===//
692
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000693 // Exherbo (2010-01-25)
694 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000695 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000696 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000697 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000698
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000699 // openSUSE 11.1 32 bit
700 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000701 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000702 // openSUSE 11.1 64 bit
703 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000704 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000705 // openSUSE 11.2
706 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000707 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000708 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000709 "x86_64-suse-linux", "", "", triple);
Rafael Espindola9d2c0602010-11-25 18:51:59 +0000710
711 // openSUSE 11.4
712 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
713 "i586-suse-linux", "", "", triple);
714 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
715 "x86_64-suse-linux", "", "", triple);
716
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000717 // Arch Linux 2008-06-24
718 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000719 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000720 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000721 "x86_64-unknown-linux-gnu", "", "", triple);
Nuno Lopes0d155a52010-09-11 17:51:45 +0000722 // Gentoo x86 2010.0 stable
723 AddGnuCPlusPlusIncludePaths(
724 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
725 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000726 // Gentoo x86 2009.1 stable
727 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000728 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000729 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000730 // Gentoo x86 2009.0 stable
731 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000732 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000733 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000734 // Gentoo x86 2008.0 stable
735 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000736 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000737 "i686-pc-linux-gnu", "", "", triple);
Nick Lewycky66935342010-07-24 21:33:13 +0000738
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000739 // Gentoo amd64 gcc 4.4.5
740 AddGnuCPlusPlusIncludePaths(
741 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4",
742 "x86_64-pc-linux-gnu", "32", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000743 // Gentoo amd64 gcc 4.4.4
744 AddGnuCPlusPlusIncludePaths(
745 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
746 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000747 // Gentoo amd64 gcc 4.4.3
748 AddGnuCPlusPlusIncludePaths(
749 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
750 "x86_64-pc-linux-gnu", "32", "", triple);
751 // Gentoo amd64 gcc 4.3.2
752 AddGnuCPlusPlusIncludePaths(
753 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
754 "x86_64-pc-linux-gnu", "", "", triple);
755 // Gentoo amd64 stable
756 AddGnuCPlusPlusIncludePaths(
757 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
758 "i686-pc-linux-gnu", "", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000759
Nick Lewycky66935342010-07-24 21:33:13 +0000760 // Gentoo amd64 llvm-gcc trunk
761 AddGnuCPlusPlusIncludePaths(
762 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
763 "x86_64-pc-linux-gnu", "", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000764
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000765 break;
766 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000767 // FreeBSD 8.0
768 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000769 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000770 break;
Anton Korobeynikovab079412010-08-31 22:39:50 +0000771 case llvm::Triple::NetBSD:
772 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
773 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000774 case llvm::Triple::OpenBSD: {
775 std::string t = triple.getTriple();
776 if (t.substr(0, 6) == "x86_64")
777 t.replace(0, 6, "amd64");
778 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
779 t, "", "", triple);
780 break;
781 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000782 case llvm::Triple::Minix:
783 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
784 "", "", "", triple);
785 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000786 case llvm::Triple::Solaris:
787 // Solaris - Fall though..
788 case llvm::Triple::AuroraUX:
789 // AuroraUX
790 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000791 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000792 break;
793 default:
794 break;
795 }
796}
797
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000798void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000799 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000800 const HeaderSearchOptions &HSOpts) {
Daniel Dunbar80c26f42010-09-09 17:38:22 +0000801 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes) {
802 if (!HSOpts.CXXSystemIncludes.empty()) {
803 for (unsigned i = 0, e = HSOpts.CXXSystemIncludes.size(); i != e; ++i)
804 AddPath(HSOpts.CXXSystemIncludes[i], System, true, false, false);
805 } else
806 AddDefaultCPlusPlusIncludePaths(triple);
807 }
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000808
mike-m79bc57c2010-05-16 19:03:52 +0000809 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000810
811 // Add the default framework include paths on Darwin.
812 if (triple.getOS() == llvm::Triple::Darwin) {
813 AddPath("/System/Library/Frameworks", System, true, false, true);
814 AddPath("/Library/Frameworks", System, true, false, true);
815 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000816}
817
Nico Weber0fca0222008-08-22 09:25:22 +0000818/// RemoveDuplicates - If there are duplicate directory entries in the specified
819/// search list, remove the later (dead) ones.
820static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
821 bool Verbose) {
822 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
823 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
824 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
825 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000826 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Chris Lattner43eee072009-02-08 01:00:10 +0000828 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Chris Lattner43eee072009-02-08 01:00:10 +0000830 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000831 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000832 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000833 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000834 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000835 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000836 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000837 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000838 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000839 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000840 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000841 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000842 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Chris Lattner30f05b52009-02-08 00:55:22 +0000845 // If we have a normal #include dir/framework/headermap that is shadowed
846 // later in the chain by a system include location, we actually want to
847 // ignore the user's request and drop the user dir... keeping the system
848 // dir. This is weird, but required to emulate GCC's search path correctly.
849 //
850 // Since dupes of system dirs are rare, just rescan to find the original
851 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000852 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000853 // Find the dir that this is the same of.
854 unsigned FirstDir;
855 for (FirstDir = 0; ; ++FirstDir) {
856 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Chris Lattner43eee072009-02-08 01:00:10 +0000858 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
859
Chris Lattner30f05b52009-02-08 00:55:22 +0000860 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000861 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000862 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Chris Lattner30f05b52009-02-08 00:55:22 +0000864 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000865 if (CurEntry.isNormalDir())
866 isSame = SearchEntry.getDir() == CurEntry.getDir();
867 else if (CurEntry.isFramework())
868 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000869 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000870 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
871 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Chris Lattner30f05b52009-02-08 00:55:22 +0000874 if (isSame)
875 break;
876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Chris Lattner30f05b52009-02-08 00:55:22 +0000878 // If the first dir in the search path is a non-system dir, zap it
879 // instead of the system one.
880 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
881 DirToRemove = FirstDir;
882 }
883
884 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000885 llvm::errs() << "ignoring duplicate directory \""
886 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +0000887 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000888 llvm::errs() << " as it is a non-system directory that duplicates "
889 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000890 }
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Chris Lattner7a739402008-09-26 17:46:45 +0000892 // This is reached if the current entry is a duplicate. Remove the
893 // DirToRemove (usually the current dir).
894 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000895 --i;
896 }
897}
898
899
900void InitHeaderSearch::Realize() {
901 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
902 std::vector<DirectoryLookup> SearchList;
903 SearchList = IncludeGroup[Angled];
904 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
905 IncludeGroup[System].end());
906 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
907 IncludeGroup[After].end());
908 RemoveDuplicates(SearchList, Verbose);
909 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Nico Weber0fca0222008-08-22 09:25:22 +0000911 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000912 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000913 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Nico Weber0fca0222008-08-22 09:25:22 +0000915
916 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
917 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
918 DontSearchCurDir);
919
920 // If verbose, print the list of directories that will be searched.
921 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000922 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000923 unsigned QuotedIdx = IncludeGroup[Quoted].size();
924 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
925 if (i == QuotedIdx)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000926 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000927 const char *Name = SearchList[i].getName();
928 const char *Suffix;
929 if (SearchList[i].isNormalDir())
930 Suffix = "";
931 else if (SearchList[i].isFramework())
932 Suffix = " (framework directory)";
933 else {
934 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
935 Suffix = " (headermap)";
936 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000937 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000938 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000939 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000940 }
941}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000942
Daniel Dunbar5814e652009-11-11 21:44:21 +0000943void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
944 const HeaderSearchOptions &HSOpts,
945 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000946 const llvm::Triple &Triple) {
947 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
948
949 // Add the user defined entries.
950 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
951 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +0000952 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
Chris Lattner23637be2010-08-24 22:27:37 +0000953 !E.IsSysRootRelative);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000954 }
955
956 // Add entries from CPATH and friends.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000957 Init.AddDelimitedPaths(HSOpts.EnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000958 if (Lang.CPlusPlus && Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000959 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000960 else if (Lang.CPlusPlus)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000961 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000962 else if (Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000963 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000964 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000965 Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000966
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000967 if (HSOpts.UseStandardIncludes)
mike-m79bc57c2010-05-16 19:03:52 +0000968 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000969
970 Init.Realize();
971}