blob: 7fa7d0162fc92bd77f90274736709ca35ec50170 [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;
Michael J. Spenceraf6530c2010-12-25 20:09:27 +000044 std::string IncludeSysroot;
45 bool IsNotEmptyOrRoot;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000046
47public:
48
Chandler Carruthc09265a2010-11-15 07:15:26 +000049 InitHeaderSearch(HeaderSearch &HS, bool verbose, llvm::StringRef sysroot)
Michael J. Spenceraf6530c2010-12-25 20:09:27 +000050 : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
51 IsNotEmptyOrRoot(!(sysroot.empty() || sysroot == "/")) {
Chandler Carruthc09265a2010-11-15 07:15:26 +000052 }
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000053
54 /// AddPath - Add the specified path to the specified group list.
Benjamin Kramere89ba592009-12-08 12:38:20 +000055 void AddPath(const llvm::Twine &Path, IncludeDirGroup Group,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000056 bool isCXXAware, bool isUserSupplied,
57 bool isFramework, bool IgnoreSysRoot = false);
58
mike-ma6087372010-05-05 17:00:31 +000059 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000060 /// libstdc++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000061 void AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
62 llvm::StringRef ArchDir,
63 llvm::StringRef Dir32,
64 llvm::StringRef Dir64,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000065 const llvm::Triple &triple);
66
Michael J. Spencer06a8dc62010-12-21 16:45:42 +000067 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000068 /// libstdc++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000069 void AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
70 llvm::StringRef Arch,
71 llvm::StringRef Version);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000072
73 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
74 /// separator. The processing follows that of the CPATH variable for gcc.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +000075 void AddDelimitedPaths(llvm::StringRef String);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000076
77 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-m79bc57c2010-05-16 19:03:52 +000078 void AddDefaultCIncludePaths(const llvm::Triple &triple,
79 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000080
81 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
82 // compiling c++.
83 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
84
85 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
86 /// that e.g. stdio.h is found.
87 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +000088 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +000089 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000090
91 /// Realize - Merges all search path lists into one list and send it to
92 /// HeaderSearch.
93 void Realize();
94};
95
96}
Nico Weber0fca0222008-08-22 09:25:22 +000097
Benjamin Kramere89ba592009-12-08 12:38:20 +000098void InitHeaderSearch::AddPath(const llvm::Twine &Path,
Benjamin Kramer458fb102009-09-05 09:49:39 +000099 IncludeDirGroup Group, bool isCXXAware,
100 bool isUserSupplied, bool isFramework,
101 bool IgnoreSysRoot) {
Benjamin Kramere89ba592009-12-08 12:38:20 +0000102 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
Nico Weber0fca0222008-08-22 09:25:22 +0000103 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Nico Weber0fca0222008-08-22 09:25:22 +0000105 // Compute the actual path, taking into consideration -isysroot.
Chandler Carruth5853b0f2010-11-15 00:05:18 +0000106 llvm::SmallString<256> MappedPathStorage;
Chandler Carruthf3721452010-11-15 00:48:13 +0000107 llvm::StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Nico Weber0fca0222008-08-22 09:25:22 +0000109 // Handle isysroot.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000110 if (Group == System && !IgnoreSysRoot &&
111 llvm::sys::path::is_absolute(MappedPathStr) &&
Michael J. Spenceraf6530c2010-12-25 20:09:27 +0000112 IsNotEmptyOrRoot) {
Chandler Carruth5619ae52010-11-15 09:28:23 +0000113 MappedPathStorage.clear();
Chandler Carruthc09265a2010-11-15 07:15:26 +0000114 MappedPathStr =
Michael J. Spenceraf6530c2010-12-25 20:09:27 +0000115 (IncludeSysroot + Path).toStringRef(MappedPathStorage);
Nico Weber0fca0222008-08-22 09:25:22 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Nico Weber0fca0222008-08-22 09:25:22 +0000118 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000119 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000120 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000121 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000122 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000123 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000124 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000125 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000126
127
Nico Weber0fca0222008-08-22 09:25:22 +0000128 // If the directory exists, add it.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000129 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000130 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
131 isFramework));
132 return;
133 }
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Nico Weber0fca0222008-08-22 09:25:22 +0000135 // Check to see if this is an apple-style headermap (which are not allowed to
136 // be frameworks).
137 if (!isFramework) {
Chris Lattner39b49bc2010-11-23 08:35:12 +0000138 if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000139 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
140 // It is a headermap, add it to the search path.
141 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
142 return;
143 }
144 }
145 }
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Nico Weber0fca0222008-08-22 09:25:22 +0000147 if (Verbose)
Chandler Carruthf3721452010-11-15 00:48:13 +0000148 llvm::errs() << "ignoring nonexistent directory \""
149 << MappedPathStr << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000150}
151
152
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000153void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) {
154 if (at.empty()) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000155 return;
156
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000157 llvm::StringRef::size_type delim;
158 while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) {
159 if (delim == 0)
Nico Weber0fca0222008-08-22 09:25:22 +0000160 AddPath(".", Angled, false, true, false);
161 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000162 AddPath(at.substr(0, delim), Angled, false, true, false);
163 at = at.substr(delim + 1);
Nico Weber0fca0222008-08-22 09:25:22 +0000164 }
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000165
166 if (at.empty())
Nico Weber0fca0222008-08-22 09:25:22 +0000167 AddPath(".", Angled, false, true, false);
168 else
169 AddPath(at, Angled, false, true, false);
170}
171
Benjamin Kramere89ba592009-12-08 12:38:20 +0000172void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
173 llvm::StringRef ArchDir,
174 llvm::StringRef Dir32,
175 llvm::StringRef Dir64,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000176 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000177 // Add the base dir
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000178 AddPath(Base, System, true, false, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000179
180 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000181 llvm::Triple::ArchType arch = triple.getArch();
182 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000183 if (is64bit)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000184 AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000185 else
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000186 AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000187
188 // Add the backward dir
189 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000190}
Nico Weber0fca0222008-08-22 09:25:22 +0000191
Benjamin Kramere89ba592009-12-08 12:38:20 +0000192void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
193 llvm::StringRef Arch,
194 llvm::StringRef Version) {
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000195 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
196 System, true, false, false);
Chris Lattner8e9006b2010-09-03 16:45:53 +0000197 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
198 System, true, false, false);
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000199 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
200 System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000201}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000202
Mike Stump620d57a2009-10-12 20:50:45 +0000203 // FIXME: This probably should goto to some platform utils place.
204#ifdef _MSC_VER
John Thompson75ee3bd2009-11-21 00:15:52 +0000205
Mike Stump620d57a2009-10-12 20:50:45 +0000206 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000207 // This also supports a means to look for high-versioned keys by use
208 // of a $VERSION placeholder in the key path.
209 // $VERSION in the key path is a placeholder for the version number,
210 // causing the highest value path to be searched for and used.
211 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
212 // There can be additional characters in the component. Only the numberic
213 // characters are compared.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000214static bool getSystemRegistryString(const char *keyPath, const char *valueName,
215 char *value, size_t maxLength) {
Mike Stump43d81762009-10-08 23:29:47 +0000216 HKEY hRootKey = NULL;
217 HKEY hKey = NULL;
218 const char* subKey = NULL;
219 DWORD valueType;
220 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000221 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000222 bool returnValue = false;
223 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
224 hRootKey = HKEY_CLASSES_ROOT;
225 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000226 }
Mike Stump43d81762009-10-08 23:29:47 +0000227 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
228 hRootKey = HKEY_USERS;
229 subKey = keyPath + 11;
230 }
231 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
232 hRootKey = HKEY_LOCAL_MACHINE;
233 subKey = keyPath + 19;
234 }
235 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
236 hRootKey = HKEY_CURRENT_USER;
237 subKey = keyPath + 18;
238 }
239 else
240 return(false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000241 const char *placeHolder = strstr(subKey, "$VERSION");
242 char bestName[256];
243 bestName[0] = '\0';
244 // If we have a $VERSION placeholder, do the highest-version search.
245 if (placeHolder) {
246 const char *keyEnd = placeHolder - 1;
247 const char *nextKey = placeHolder;
248 // Find end of previous key.
249 while ((keyEnd > subKey) && (*keyEnd != '\\'))
250 keyEnd--;
251 // Find end of key containing $VERSION.
252 while (*nextKey && (*nextKey != '\\'))
253 nextKey++;
254 size_t partialKeyLength = keyEnd - subKey;
255 char partialKey[256];
256 if (partialKeyLength > sizeof(partialKey))
257 partialKeyLength = sizeof(partialKey);
258 strncpy(partialKey, subKey, partialKeyLength);
259 partialKey[partialKeyLength] = '\0';
260 HKEY hTopKey = NULL;
261 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
262 if (lResult == ERROR_SUCCESS) {
263 char keyName[256];
264 int bestIndex = -1;
265 double bestValue = 0.0;
266 DWORD index, size = sizeof(keyName) - 1;
Nuno Lopes33cc2432009-12-07 17:18:48 +0000267 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
268 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
269 const char *sp = keyName;
270 while (*sp && !isdigit(*sp))
271 sp++;
272 if (!*sp)
273 continue;
274 const char *ep = sp + 1;
275 while (*ep && (isdigit(*ep) || (*ep == '.')))
276 ep++;
277 char numBuf[32];
278 strncpy(numBuf, sp, sizeof(numBuf) - 1);
279 numBuf[sizeof(numBuf) - 1] = '\0';
280 double value = strtod(numBuf, NULL);
281 if (value > bestValue) {
282 bestIndex = (int)index;
283 bestValue = value;
284 strcpy(bestName, keyName);
285 }
John Thompson75ee3bd2009-11-21 00:15:52 +0000286 size = sizeof(keyName) - 1;
287 }
288 // If we found the highest versioned key, open the key and get the value.
289 if (bestIndex != -1) {
290 // Append rest of key.
291 strncat(bestName, nextKey, sizeof(bestName) - 1);
292 bestName[sizeof(bestName) - 1] = '\0';
293 // Open the chosen key path remainder.
294 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
295 if (lResult == ERROR_SUCCESS) {
296 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
297 (LPBYTE)value, &valueSize);
298 if (lResult == ERROR_SUCCESS)
299 returnValue = true;
300 RegCloseKey(hKey);
301 }
302 }
303 RegCloseKey(hTopKey);
304 }
305 }
306 else {
307 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
308 if (lResult == ERROR_SUCCESS) {
309 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
310 (LPBYTE)value, &valueSize);
311 if (lResult == ERROR_SUCCESS)
312 returnValue = true;
313 RegCloseKey(hKey);
314 }
Mike Stump43d81762009-10-08 23:29:47 +0000315 }
316 return(returnValue);
317}
Mike Stump620d57a2009-10-12 20:50:45 +0000318#else // _MSC_VER
319 // Read registry string.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000320static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
Mike Stump620d57a2009-10-12 20:50:45 +0000321 return(false);
322}
323#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000324
Mike Stump43d81762009-10-08 23:29:47 +0000325 // Get Visual Studio installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000326static bool getVisualStudioDir(std::string &path) {
Michael J. Spencerff58e362010-08-21 21:55:07 +0000327 // First check the environment variables that vsvars32.bat sets.
328 const char* vcinstalldir = getenv("VCINSTALLDIR");
329 if(vcinstalldir) {
330 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
331 if (p)
332 *p = '\0';
333 path = vcinstalldir;
334 return(true);
335 }
336
John Thompson75ee3bd2009-11-21 00:15:52 +0000337 char vsIDEInstallDir[256];
Douglas Gregor80f93d92010-05-18 05:47:04 +0000338 char vsExpressIDEInstallDir[256];
Michael J. Spencerff58e362010-08-21 21:55:07 +0000339 // Then try the windows registry.
John Thompson75ee3bd2009-11-21 00:15:52 +0000340 bool hasVCDir = getSystemRegistryString(
341 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
342 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Douglas Gregor80f93d92010-05-18 05:47:04 +0000343 bool hasVCExpressDir = getSystemRegistryString(
344 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
345 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000346 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000347 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000348 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
349 if (p)
350 *p = '\0';
351 path = vsIDEInstallDir;
352 return(true);
353 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000354 else if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
355 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
356 if (p)
357 *p = '\0';
358 path = vsExpressIDEInstallDir;
359 return(true);
360 }
Mike Stump620d57a2009-10-12 20:50:45 +0000361 else {
362 // Try the environment.
Douglas Gregor80f93d92010-05-18 05:47:04 +0000363 const char* vs100comntools = getenv("VS100COMNTOOLS");
Mike Stump620d57a2009-10-12 20:50:45 +0000364 const char* vs90comntools = getenv("VS90COMNTOOLS");
365 const char* vs80comntools = getenv("VS80COMNTOOLS");
366 const char* vscomntools = NULL;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000367
368 // Try to find the version that we were compiled with
369 if(false) {}
370 #if (_MSC_VER >= 1600) // VC100
371 else if(vs100comntools) {
372 vscomntools = vs100comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000373 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000374 #elif (_MSC_VER == 1500) // VC80
375 else if(vs90comntools) {
376 vscomntools = vs90comntools;
377 }
378 #elif (_MSC_VER == 1400) // VC80
379 else if(vs80comntools) {
380 vscomntools = vs80comntools;
381 }
382 #endif
383 // Otherwise find any version we can
384 else if (vs100comntools)
385 vscomntools = vs100comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000386 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000387 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000388 else if (vs80comntools)
389 vscomntools = vs80comntools;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000390
Mike Stump620d57a2009-10-12 20:50:45 +0000391 if (vscomntools && *vscomntools) {
Dan Gohmancb421fa2010-04-19 16:39:44 +0000392 char *p = const_cast<char *>(strstr(vscomntools, "\\Common7\\Tools"));
Mike Stump620d57a2009-10-12 20:50:45 +0000393 if (p)
394 *p = '\0';
395 path = vscomntools;
396 return(true);
397 }
398 else
399 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000400 }
Mike Stump620d57a2009-10-12 20:50:45 +0000401 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000402}
Mike Stump43d81762009-10-08 23:29:47 +0000403
John Thompson75ee3bd2009-11-21 00:15:52 +0000404 // Get Windows SDK installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000405static bool getWindowsSDKDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000406 char windowsSDKInstallDir[256];
407 // Try the Windows registry.
408 bool hasSDKDir = getSystemRegistryString(
409 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
410 "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1);
411 // If we have both vc80 and vc90, pick version we were compiled with.
412 if (hasSDKDir && windowsSDKInstallDir[0]) {
413 path = windowsSDKInstallDir;
414 return(true);
415 }
416 return(false);
417}
418
mike-m79bc57c2010-05-16 19:03:52 +0000419void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
420 const HeaderSearchOptions &HSOpts) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000421 llvm::Triple::OSType os = triple.getOS();
422
423 switch (os) {
424 case llvm::Triple::NetBSD:
425 break;
426 default:
427 // FIXME: temporary hack: hard-coded paths.
428 AddPath("/usr/local/include", System, true, false, false);
429 break;
430 }
mike-m79bc57c2010-05-16 19:03:52 +0000431
432 // Builtin includes use #include_next directives and should be positioned
433 // just prior C include dirs.
434 if (HSOpts.UseBuiltinIncludes) {
435 // Ignore the sys root, we *always* look for clang headers relative to
436 // supplied path.
437 llvm::sys::Path P(HSOpts.ResourceDir);
438 P.appendComponent("include");
439 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
440 }
441
442 // Add dirs specified via 'configure --with-c-include-dirs'.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000443 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
444 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000445 llvm::SmallVector<llvm::StringRef, 5> dirs;
446 CIncludeDirs.split(dirs, ":");
447 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
448 i != dirs.end();
449 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000450 AddPath(*i, System, false, false, false);
451 return;
452 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000453
Mike Stump43d81762009-10-08 23:29:47 +0000454 switch (os) {
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000455 case llvm::Triple::Win32: {
456 std::string VSDir;
457 std::string WindowsSDKDir;
458 if (getVisualStudioDir(VSDir)) {
459 AddPath(VSDir + "\\VC\\include", System, false, false, false);
460 if (getWindowsSDKDir(WindowsSDKDir))
461 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
462 else
463 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
464 System, false, false, false);
465 } else {
466 // Default install paths.
467 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
468 System, false, false, false);
469 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
470 System, false, false, false);
471 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000472 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000473 System, false, false, false);
474 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
475 System, false, false, false);
476 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000477 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000478 System, false, false, false);
479 // For some clang developers.
480 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
481 System, false, false, false);
482 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000483 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000484 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000485 }
486 break;
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000487 }
Chris Lattner86ed3a32010-04-11 19:29:39 +0000488 case llvm::Triple::Haiku:
489 AddPath("/boot/common/include", System, true, false, false);
490 AddPath("/boot/develop/headers/os", System, true, false, false);
491 AddPath("/boot/develop/headers/os/app", System, true, false, false);
492 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
493 AddPath("/boot/develop/headers/os/device", System, true, false, false);
494 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
495 AddPath("/boot/develop/headers/os/game", System, true, false, false);
496 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
497 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
498 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
499 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
500 AddPath("/boot/develop/headers/os/media", System, true, false, false);
501 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
502 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
503 AddPath("/boot/develop/headers/os/net", System, true, false, false);
504 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
505 AddPath("/boot/develop/headers/os/support", System, true, false, false);
506 AddPath("/boot/develop/headers/os/translation",
507 System, true, false, false);
508 AddPath("/boot/develop/headers/os/add-ons/graphics",
509 System, true, false, false);
510 AddPath("/boot/develop/headers/os/add-ons/input_server",
511 System, true, false, false);
512 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
513 System, true, false, false);
514 AddPath("/boot/develop/headers/os/add-ons/tracker",
515 System, true, false, false);
516 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
517 System, true, false, false);
518 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
519 System, true, false, false);
520 AddPath("/boot/develop/headers/os/be_apps/Tracker",
521 System, true, false, false);
522 AddPath("/boot/develop/headers/cpp", System, true, false, false);
523 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
524 System, true, false, false);
525 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
526 AddPath("/boot/develop/headers/bsd", System, true, false, false);
527 AddPath("/boot/develop/headers/glibc", System, true, false, false);
528 AddPath("/boot/develop/headers/posix", System, true, false, false);
529 AddPath("/boot/develop/headers", System, true, false, false);
Eli Friedmana7e68452010-08-22 01:00:03 +0000530 break;
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000531 case llvm::Triple::Cygwin:
532 AddPath("/usr/include/w32api", System, true, false, false);
533 break;
Mike Stump43d81762009-10-08 23:29:47 +0000534 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000535 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000536 AddPath("c:/mingw/include", System, true, false, false);
537 break;
538 default:
Mike Stump43d81762009-10-08 23:29:47 +0000539 break;
540 }
John Thompsond3f88342009-10-13 18:51:32 +0000541
John Thompsond3f88342009-10-13 18:51:32 +0000542 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000543}
544
Chris Lattner0e3cc052010-05-05 05:28:39 +0000545void InitHeaderSearch::
546AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000547 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000548 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
549 if (CxxIncludeRoot != "") {
550 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
551 if (CxxIncludeArch == "")
552 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
Chris Lattner0e3cc052010-05-05 05:28:39 +0000553 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
554 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000555 else
556 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
Chris Lattner0e3cc052010-05-05 05:28:39 +0000557 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
558 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000559 return;
560 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000561 // FIXME: temporary hack: hard-coded paths.
562 switch (os) {
563 case llvm::Triple::Cygwin:
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000564 // Cygwin-1.7
565 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
566 // g++-4 / Cygwin-1.5
567 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
568 // FIXME: Do we support g++-3.4.4?
569 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000570 break;
571 case llvm::Triple::MinGW64:
Chris Lattner6a081402010-09-01 15:51:58 +0000572 // Try gcc 4.5.0
573 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.5.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000574 // Try gcc 4.4.0
575 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
576 // Try gcc 4.3.0
577 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
578 // Fall through.
579 case llvm::Triple::MinGW32:
Chris Lattner6a081402010-09-01 15:51:58 +0000580 // Try gcc 4.5.0
581 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000582 // Try gcc 4.4.0
583 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
584 // Try gcc 4.3.0
585 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
586 break;
587 case llvm::Triple::Darwin:
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000588 switch (triple.getArch()) {
589 default: break;
590
Douglas Gregor582c3012010-05-29 01:15:12 +0000591 case llvm::Triple::ppc:
592 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000593 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
594 "powerpc-apple-darwin10", "", "ppc64",
595 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000596 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
597 "powerpc-apple-darwin10", "", "ppc64",
598 triple);
599 break;
600
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000601 case llvm::Triple::x86:
602 case llvm::Triple::x86_64:
603 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
604 "i686-apple-darwin10", "", "x86_64", triple);
605 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
606 "i686-apple-darwin8", "", "", triple);
607 break;
608
609 case llvm::Triple::arm:
610 case llvm::Triple::thumb:
611 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
612 "arm-apple-darwin10", "v7", "", triple);
613 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
614 "arm-apple-darwin10", "v6", "", triple);
615 break;
616 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000617 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000618 case llvm::Triple::DragonFly:
619 AddPath("/usr/include/c++/4.1", System, true, false, false);
620 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000621 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000622 //===------------------------------------------------------------------===//
623 // Debian based distros.
624 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
625 //===------------------------------------------------------------------===//
626 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
627 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
628 // Debian 6.0 "squeeze" -- gcc-4.4.2
629 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
630 "x86_64-linux-gnu", "32", "", triple);
631 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
632 "i486-linux-gnu", "", "64", triple);
Nick Lewyckydb083552011-02-01 21:32:14 +0000633 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
634 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000635 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
636 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
637 // Debian 5.0 "lenny" -- gcc-4.3.2
638 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
639 "x86_64-linux-gnu", "32", "", triple);
640 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
641 "i486-linux-gnu", "", "64", triple);
Rafael Espindolae7d6c2c2010-06-04 14:28:10 +0000642 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
643 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000644 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
645 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
646 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
647 "x86_64-linux-gnu", "32", "", triple);
648 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
649 "i486-linux-gnu", "", "64", triple);
650 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
651 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
652 "x86_64-linux-gnu", "32", "", triple);
653 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
654 "i486-linux-gnu", "", "64", triple);
655
656 //===------------------------------------------------------------------===//
657 // Redhat based distros.
658 //===------------------------------------------------------------------===//
Rafael Espindola6638b3a2010-11-02 18:39:34 +0000659 // Fedora 14
660 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
661 "x86_64-redhat-linux", "32", "", triple);
662 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
663 "i686-redhat-linux", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000664 // Fedora 13
Chris Lattner4336e192010-05-29 01:01:38 +0000665 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
666 "x86_64-redhat-linux", "32", "", triple);
667 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
668 "i686-redhat-linux","", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000669 // Fedora 12
670 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
671 "x86_64-redhat-linux", "32", "", triple);
672 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
673 "i686-redhat-linux","", "", triple);
674 // Fedora 12 (pre-FEB-2010)
675 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
676 "x86_64-redhat-linux", "32", "", triple);
677 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
678 "i686-redhat-linux","", "", triple);
679 // Fedora 11
680 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
681 "x86_64-redhat-linux", "32", "", triple);
682 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
683 "i586-redhat-linux","", "", triple);
684 // Fedora 10
685 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
686 "x86_64-redhat-linux", "32", "", triple);
687 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
688 "i386-redhat-linux","", "", triple);
689 // Fedora 9
690 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
691 "x86_64-redhat-linux", "32", "", triple);
692 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
693 "i386-redhat-linux", "", "", triple);
694 // Fedora 8
695 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
696 "x86_64-redhat-linux", "", "", triple);
697 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
698 "i386-redhat-linux", "", "", triple);
699
700 //===------------------------------------------------------------------===//
701
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000702 // Exherbo (2010-01-25)
703 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000704 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000705 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000706 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000707
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000708 // openSUSE 11.1 32 bit
709 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000710 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000711 // openSUSE 11.1 64 bit
712 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000713 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000714 // openSUSE 11.2
715 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000716 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000717 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000718 "x86_64-suse-linux", "", "", triple);
Rafael Espindola9d2c0602010-11-25 18:51:59 +0000719
720 // openSUSE 11.4
721 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
722 "i586-suse-linux", "", "", triple);
723 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
724 "x86_64-suse-linux", "", "", triple);
725
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000726 // Arch Linux 2008-06-24
727 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000728 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000729 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000730 "x86_64-unknown-linux-gnu", "", "", triple);
Nuno Lopes0d155a52010-09-11 17:51:45 +0000731 // Gentoo x86 2010.0 stable
732 AddGnuCPlusPlusIncludePaths(
733 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
734 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000735 // Gentoo x86 2009.1 stable
736 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000737 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000738 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000739 // Gentoo x86 2009.0 stable
740 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000741 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000742 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000743 // Gentoo x86 2008.0 stable
744 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000745 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000746 "i686-pc-linux-gnu", "", "", triple);
Nick Lewycky66935342010-07-24 21:33:13 +0000747
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000748 // Gentoo amd64 gcc 4.4.5
749 AddGnuCPlusPlusIncludePaths(
750 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4",
751 "x86_64-pc-linux-gnu", "32", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000752 // Gentoo amd64 gcc 4.4.4
753 AddGnuCPlusPlusIncludePaths(
754 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
755 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000756 // Gentoo amd64 gcc 4.4.3
757 AddGnuCPlusPlusIncludePaths(
758 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
759 "x86_64-pc-linux-gnu", "32", "", triple);
760 // Gentoo amd64 gcc 4.3.2
761 AddGnuCPlusPlusIncludePaths(
762 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
763 "x86_64-pc-linux-gnu", "", "", triple);
764 // Gentoo amd64 stable
765 AddGnuCPlusPlusIncludePaths(
766 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
767 "i686-pc-linux-gnu", "", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000768
Nick Lewycky66935342010-07-24 21:33:13 +0000769 // Gentoo amd64 llvm-gcc trunk
770 AddGnuCPlusPlusIncludePaths(
771 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
772 "x86_64-pc-linux-gnu", "", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000773
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000774 break;
775 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000776 // FreeBSD 8.0
777 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000778 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000779 break;
Anton Korobeynikovab079412010-08-31 22:39:50 +0000780 case llvm::Triple::NetBSD:
781 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
782 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000783 case llvm::Triple::OpenBSD: {
784 std::string t = triple.getTriple();
785 if (t.substr(0, 6) == "x86_64")
786 t.replace(0, 6, "amd64");
787 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
788 t, "", "", triple);
789 break;
790 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000791 case llvm::Triple::Minix:
792 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
793 "", "", "", triple);
794 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000795 case llvm::Triple::Solaris:
796 // Solaris - Fall though..
797 case llvm::Triple::AuroraUX:
798 // AuroraUX
799 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000800 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000801 break;
802 default:
803 break;
804 }
805}
806
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000807void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000808 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000809 const HeaderSearchOptions &HSOpts) {
Daniel Dunbar80c26f42010-09-09 17:38:22 +0000810 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes) {
811 if (!HSOpts.CXXSystemIncludes.empty()) {
812 for (unsigned i = 0, e = HSOpts.CXXSystemIncludes.size(); i != e; ++i)
813 AddPath(HSOpts.CXXSystemIncludes[i], System, true, false, false);
814 } else
815 AddDefaultCPlusPlusIncludePaths(triple);
816 }
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000817
mike-m79bc57c2010-05-16 19:03:52 +0000818 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000819
820 // Add the default framework include paths on Darwin.
821 if (triple.getOS() == llvm::Triple::Darwin) {
822 AddPath("/System/Library/Frameworks", System, true, false, true);
823 AddPath("/Library/Frameworks", System, true, false, true);
824 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000825}
826
Nico Weber0fca0222008-08-22 09:25:22 +0000827/// RemoveDuplicates - If there are duplicate directory entries in the specified
828/// search list, remove the later (dead) ones.
829static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
830 bool Verbose) {
831 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
832 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
833 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
834 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000835 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Chris Lattner43eee072009-02-08 01:00:10 +0000837 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Chris Lattner43eee072009-02-08 01:00:10 +0000839 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000840 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000841 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000842 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000843 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000844 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000845 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000846 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000847 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000848 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000849 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000850 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000851 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chris Lattner30f05b52009-02-08 00:55:22 +0000854 // If we have a normal #include dir/framework/headermap that is shadowed
855 // later in the chain by a system include location, we actually want to
856 // ignore the user's request and drop the user dir... keeping the system
857 // dir. This is weird, but required to emulate GCC's search path correctly.
858 //
859 // Since dupes of system dirs are rare, just rescan to find the original
860 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000861 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000862 // Find the dir that this is the same of.
863 unsigned FirstDir;
864 for (FirstDir = 0; ; ++FirstDir) {
865 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Chris Lattner43eee072009-02-08 01:00:10 +0000867 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
868
Chris Lattner30f05b52009-02-08 00:55:22 +0000869 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000870 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000871 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Chris Lattner30f05b52009-02-08 00:55:22 +0000873 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000874 if (CurEntry.isNormalDir())
875 isSame = SearchEntry.getDir() == CurEntry.getDir();
876 else if (CurEntry.isFramework())
877 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000878 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000879 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
880 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Chris Lattner30f05b52009-02-08 00:55:22 +0000883 if (isSame)
884 break;
885 }
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Chris Lattner30f05b52009-02-08 00:55:22 +0000887 // If the first dir in the search path is a non-system dir, zap it
888 // instead of the system one.
889 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
890 DirToRemove = FirstDir;
891 }
892
893 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000894 llvm::errs() << "ignoring duplicate directory \""
895 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +0000896 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000897 llvm::errs() << " as it is a non-system directory that duplicates "
898 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000899 }
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Chris Lattner7a739402008-09-26 17:46:45 +0000901 // This is reached if the current entry is a duplicate. Remove the
902 // DirToRemove (usually the current dir).
903 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000904 --i;
905 }
906}
907
908
909void InitHeaderSearch::Realize() {
910 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
911 std::vector<DirectoryLookup> SearchList;
912 SearchList = IncludeGroup[Angled];
913 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
914 IncludeGroup[System].end());
915 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
916 IncludeGroup[After].end());
917 RemoveDuplicates(SearchList, Verbose);
918 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Nico Weber0fca0222008-08-22 09:25:22 +0000920 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000921 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000922 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Nico Weber0fca0222008-08-22 09:25:22 +0000924
925 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
926 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
927 DontSearchCurDir);
928
929 // If verbose, print the list of directories that will be searched.
930 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000931 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000932 unsigned QuotedIdx = IncludeGroup[Quoted].size();
933 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
934 if (i == QuotedIdx)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000935 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000936 const char *Name = SearchList[i].getName();
937 const char *Suffix;
938 if (SearchList[i].isNormalDir())
939 Suffix = "";
940 else if (SearchList[i].isFramework())
941 Suffix = " (framework directory)";
942 else {
943 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
944 Suffix = " (headermap)";
945 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000946 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000947 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000948 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000949 }
950}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000951
Daniel Dunbar5814e652009-11-11 21:44:21 +0000952void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
953 const HeaderSearchOptions &HSOpts,
954 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000955 const llvm::Triple &Triple) {
956 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
957
958 // Add the user defined entries.
959 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
960 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +0000961 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
Chris Lattner23637be2010-08-24 22:27:37 +0000962 !E.IsSysRootRelative);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000963 }
964
965 // Add entries from CPATH and friends.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000966 Init.AddDelimitedPaths(HSOpts.EnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000967 if (Lang.CPlusPlus && Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000968 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000969 else if (Lang.CPlusPlus)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000970 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000971 else if (Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000972 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000973 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000974 Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000975
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000976 if (HSOpts.UseStandardIncludes)
mike-m79bc57c2010-05-16 19:03:52 +0000977 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000978
979 Init.Realize();
980}