blob: 855a9bed4c360b77f6a3f180f268fb278d9b4b60 [file] [log] [blame]
Nick Lewyckydf22c2c2010-07-25 03:12:58 +00001//===--- InitHeaderSearch.cpp - Initialize header search paths ------------===//
Nico Weber0fca0222008-08-22 09:25:22 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the InitHeaderSearch class.
11//
12//===----------------------------------------------------------------------===//
13
Oscar Fuentes2100fe92011-02-03 22:48:20 +000014#ifdef HAVE_CLANG_CONFIG_H
15# include "clang/Config/config.h"
16#endif
17
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000018#include "clang/Frontend/Utils.h"
Nico Weber0fca0222008-08-22 09:25:22 +000019#include "clang/Basic/FileManager.h"
20#include "clang/Basic/LangOptions.h"
Daniel Dunbar63c8b772009-11-07 04:20:50 +000021#include "clang/Frontend/HeaderSearchOptions.h"
22#include "clang/Lex/HeaderSearch.h"
Nico Weber0fca0222008-08-22 09:25:22 +000023#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/SmallPtrSet.h"
Rafael Espindolaaadd7a42009-11-13 05:13:58 +000025#include "llvm/ADT/SmallVector.h"
Rafael Espindolaf0a2f512009-11-12 05:48:41 +000026#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000027#include "llvm/ADT/Triple.h"
Benjamin Kramere89ba592009-12-08 12:38:20 +000028#include "llvm/ADT/Twine.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000029#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000030#include "llvm/Support/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000031#include "llvm/Config/config.h"
Mike Stump620d57a2009-10-12 20:50:45 +000032#ifdef _MSC_VER
33 #define WIN32_LEAN_AND_MEAN 1
34 #include <windows.h>
35#endif
Nico Weber0fca0222008-08-22 09:25:22 +000036using namespace clang;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000037using namespace clang::frontend;
38
39namespace {
40
41/// InitHeaderSearch - This class makes it easier to set the search paths of
42/// a HeaderSearch object. InitHeaderSearch stores several search path lists
43/// internally, which can be sent to a HeaderSearch object in one swoop.
44class InitHeaderSearch {
Joerg Sonnenberger2df66472011-02-22 00:40:56 +000045 std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath;
46 typedef std::vector<std::pair<IncludeDirGroup,
47 DirectoryLookup> >::const_iterator path_iterator;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000048 HeaderSearch& Headers;
49 bool Verbose;
Michael J. Spenceraf6530c2010-12-25 20:09:27 +000050 std::string IncludeSysroot;
51 bool IsNotEmptyOrRoot;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000052
53public:
54
Chandler Carruthc09265a2010-11-15 07:15:26 +000055 InitHeaderSearch(HeaderSearch &HS, bool verbose, llvm::StringRef sysroot)
Michael J. Spenceraf6530c2010-12-25 20:09:27 +000056 : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
57 IsNotEmptyOrRoot(!(sysroot.empty() || sysroot == "/")) {
Chandler Carruthc09265a2010-11-15 07:15:26 +000058 }
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000059
60 /// AddPath - Add the specified path to the specified group list.
Benjamin Kramere89ba592009-12-08 12:38:20 +000061 void AddPath(const llvm::Twine &Path, IncludeDirGroup Group,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000062 bool isCXXAware, bool isUserSupplied,
63 bool isFramework, bool IgnoreSysRoot = false);
64
mike-ma6087372010-05-05 17:00:31 +000065 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000066 /// libstdc++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000067 void AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
68 llvm::StringRef ArchDir,
69 llvm::StringRef Dir32,
70 llvm::StringRef Dir64,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000071 const llvm::Triple &triple);
72
Michael J. Spencer06a8dc62010-12-21 16:45:42 +000073 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000074 /// libstdc++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000075 void AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
76 llvm::StringRef Arch,
77 llvm::StringRef Version);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000078
79 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
80 /// separator. The processing follows that of the CPATH variable for gcc.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +000081 void AddDelimitedPaths(llvm::StringRef String);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000082
83 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-m79bc57c2010-05-16 19:03:52 +000084 void AddDefaultCIncludePaths(const llvm::Triple &triple,
85 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000086
87 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
88 // compiling c++.
89 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
90
91 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
92 /// that e.g. stdio.h is found.
93 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +000094 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +000095 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000096
97 /// Realize - Merges all search path lists into one list and send it to
98 /// HeaderSearch.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +000099 void Realize(const LangOptions &Lang);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000100};
101
102}
Nico Weber0fca0222008-08-22 09:25:22 +0000103
Benjamin Kramere89ba592009-12-08 12:38:20 +0000104void InitHeaderSearch::AddPath(const llvm::Twine &Path,
Benjamin Kramer458fb102009-09-05 09:49:39 +0000105 IncludeDirGroup Group, bool isCXXAware,
106 bool isUserSupplied, bool isFramework,
107 bool IgnoreSysRoot) {
Benjamin Kramere89ba592009-12-08 12:38:20 +0000108 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
Nico Weber0fca0222008-08-22 09:25:22 +0000109 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Nico Weber0fca0222008-08-22 09:25:22 +0000111 // Compute the actual path, taking into consideration -isysroot.
Chandler Carruth5853b0f2010-11-15 00:05:18 +0000112 llvm::SmallString<256> MappedPathStorage;
Chandler Carruthf3721452010-11-15 00:48:13 +0000113 llvm::StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Nico Weber0fca0222008-08-22 09:25:22 +0000115 // Handle isysroot.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000116 if (Group == System && !IgnoreSysRoot &&
117 llvm::sys::path::is_absolute(MappedPathStr) &&
Michael J. Spenceraf6530c2010-12-25 20:09:27 +0000118 IsNotEmptyOrRoot) {
Chandler Carruth5619ae52010-11-15 09:28:23 +0000119 MappedPathStorage.clear();
Chandler Carruthc09265a2010-11-15 07:15:26 +0000120 MappedPathStr =
Michael J. Spenceraf6530c2010-12-25 20:09:27 +0000121 (IncludeSysroot + Path).toStringRef(MappedPathStorage);
Nico Weber0fca0222008-08-22 09:25:22 +0000122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Nico Weber0fca0222008-08-22 09:25:22 +0000124 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000125 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000126 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000127 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000128 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000129 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000130 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000131 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
133
Nico Weber0fca0222008-08-22 09:25:22 +0000134 // If the directory exists, add it.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000135 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000136 IncludePath.push_back(std::make_pair(Group, DirectoryLookup(DE, Type,
137 isUserSupplied, isFramework)));
Nico Weber0fca0222008-08-22 09:25:22 +0000138 return;
139 }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Nico Weber0fca0222008-08-22 09:25:22 +0000141 // Check to see if this is an apple-style headermap (which are not allowed to
142 // be frameworks).
143 if (!isFramework) {
Chris Lattner39b49bc2010-11-23 08:35:12 +0000144 if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000145 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
146 // It is a headermap, add it to the search path.
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000147 IncludePath.push_back(std::make_pair(Group, DirectoryLookup(HM, Type,
148 isUserSupplied)));
Nico Weber0fca0222008-08-22 09:25:22 +0000149 return;
150 }
151 }
152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Nico Weber0fca0222008-08-22 09:25:22 +0000154 if (Verbose)
Chandler Carruthf3721452010-11-15 00:48:13 +0000155 llvm::errs() << "ignoring nonexistent directory \""
156 << MappedPathStr << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000157}
158
159
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000160void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) {
161 if (at.empty()) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000162 return;
163
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000164 llvm::StringRef::size_type delim;
165 while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) {
166 if (delim == 0)
Nico Weber0fca0222008-08-22 09:25:22 +0000167 AddPath(".", Angled, false, true, false);
168 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000169 AddPath(at.substr(0, delim), Angled, false, true, false);
170 at = at.substr(delim + 1);
Nico Weber0fca0222008-08-22 09:25:22 +0000171 }
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000172
173 if (at.empty())
Nico Weber0fca0222008-08-22 09:25:22 +0000174 AddPath(".", Angled, false, true, false);
175 else
176 AddPath(at, Angled, false, true, false);
177}
178
Benjamin Kramere89ba592009-12-08 12:38:20 +0000179void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
180 llvm::StringRef ArchDir,
181 llvm::StringRef Dir32,
182 llvm::StringRef Dir64,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000183 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000184 // Add the base dir
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000185 AddPath(Base, CXXSystem, true, false, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000186
187 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000188 llvm::Triple::ArchType arch = triple.getArch();
189 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000190 if (is64bit)
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000191 AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000192 else
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000193 AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, true, false, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000194
195 // Add the backward dir
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000196 AddPath(Base + "/backward", CXXSystem, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000197}
Nico Weber0fca0222008-08-22 09:25:22 +0000198
Benjamin Kramere89ba592009-12-08 12:38:20 +0000199void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
200 llvm::StringRef Arch,
201 llvm::StringRef Version) {
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000202 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000203 CXXSystem, true, false, false);
Chris Lattner8e9006b2010-09-03 16:45:53 +0000204 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000205 CXXSystem, true, false, false);
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000206 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000207 CXXSystem, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000208}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000209
Mike Stump620d57a2009-10-12 20:50:45 +0000210 // FIXME: This probably should goto to some platform utils place.
211#ifdef _MSC_VER
John Thompson75ee3bd2009-11-21 00:15:52 +0000212
Mike Stump620d57a2009-10-12 20:50:45 +0000213 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000214 // This also supports a means to look for high-versioned keys by use
215 // of a $VERSION placeholder in the key path.
216 // $VERSION in the key path is a placeholder for the version number,
217 // causing the highest value path to be searched for and used.
218 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
219 // There can be additional characters in the component. Only the numberic
220 // characters are compared.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000221static bool getSystemRegistryString(const char *keyPath, const char *valueName,
222 char *value, size_t maxLength) {
Mike Stump43d81762009-10-08 23:29:47 +0000223 HKEY hRootKey = NULL;
224 HKEY hKey = NULL;
225 const char* subKey = NULL;
226 DWORD valueType;
227 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000228 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000229 bool returnValue = false;
230 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
231 hRootKey = HKEY_CLASSES_ROOT;
232 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000233 }
Mike Stump43d81762009-10-08 23:29:47 +0000234 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
235 hRootKey = HKEY_USERS;
236 subKey = keyPath + 11;
237 }
238 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
239 hRootKey = HKEY_LOCAL_MACHINE;
240 subKey = keyPath + 19;
241 }
242 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
243 hRootKey = HKEY_CURRENT_USER;
244 subKey = keyPath + 18;
245 }
246 else
247 return(false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000248 const char *placeHolder = strstr(subKey, "$VERSION");
249 char bestName[256];
250 bestName[0] = '\0';
251 // If we have a $VERSION placeholder, do the highest-version search.
252 if (placeHolder) {
253 const char *keyEnd = placeHolder - 1;
254 const char *nextKey = placeHolder;
255 // Find end of previous key.
256 while ((keyEnd > subKey) && (*keyEnd != '\\'))
257 keyEnd--;
258 // Find end of key containing $VERSION.
259 while (*nextKey && (*nextKey != '\\'))
260 nextKey++;
261 size_t partialKeyLength = keyEnd - subKey;
262 char partialKey[256];
263 if (partialKeyLength > sizeof(partialKey))
264 partialKeyLength = sizeof(partialKey);
265 strncpy(partialKey, subKey, partialKeyLength);
266 partialKey[partialKeyLength] = '\0';
267 HKEY hTopKey = NULL;
268 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
269 if (lResult == ERROR_SUCCESS) {
270 char keyName[256];
271 int bestIndex = -1;
272 double bestValue = 0.0;
273 DWORD index, size = sizeof(keyName) - 1;
Nuno Lopes33cc2432009-12-07 17:18:48 +0000274 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
275 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
276 const char *sp = keyName;
277 while (*sp && !isdigit(*sp))
278 sp++;
279 if (!*sp)
280 continue;
281 const char *ep = sp + 1;
282 while (*ep && (isdigit(*ep) || (*ep == '.')))
283 ep++;
284 char numBuf[32];
285 strncpy(numBuf, sp, sizeof(numBuf) - 1);
286 numBuf[sizeof(numBuf) - 1] = '\0';
287 double value = strtod(numBuf, NULL);
288 if (value > bestValue) {
289 bestIndex = (int)index;
290 bestValue = value;
291 strcpy(bestName, keyName);
292 }
John Thompson75ee3bd2009-11-21 00:15:52 +0000293 size = sizeof(keyName) - 1;
294 }
295 // If we found the highest versioned key, open the key and get the value.
296 if (bestIndex != -1) {
297 // Append rest of key.
298 strncat(bestName, nextKey, sizeof(bestName) - 1);
299 bestName[sizeof(bestName) - 1] = '\0';
300 // Open the chosen key path remainder.
301 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
302 if (lResult == ERROR_SUCCESS) {
303 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
304 (LPBYTE)value, &valueSize);
305 if (lResult == ERROR_SUCCESS)
306 returnValue = true;
307 RegCloseKey(hKey);
308 }
309 }
310 RegCloseKey(hTopKey);
311 }
312 }
313 else {
314 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
315 if (lResult == ERROR_SUCCESS) {
316 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
317 (LPBYTE)value, &valueSize);
318 if (lResult == ERROR_SUCCESS)
319 returnValue = true;
320 RegCloseKey(hKey);
321 }
Mike Stump43d81762009-10-08 23:29:47 +0000322 }
323 return(returnValue);
324}
Mike Stump620d57a2009-10-12 20:50:45 +0000325#else // _MSC_VER
326 // Read registry string.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000327static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
Mike Stump620d57a2009-10-12 20:50:45 +0000328 return(false);
329}
330#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000331
Mike Stump43d81762009-10-08 23:29:47 +0000332 // Get Visual Studio installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000333static bool getVisualStudioDir(std::string &path) {
Michael J. Spencerff58e362010-08-21 21:55:07 +0000334 // First check the environment variables that vsvars32.bat sets.
335 const char* vcinstalldir = getenv("VCINSTALLDIR");
336 if(vcinstalldir) {
337 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
338 if (p)
339 *p = '\0';
340 path = vcinstalldir;
341 return(true);
342 }
343
John Thompson75ee3bd2009-11-21 00:15:52 +0000344 char vsIDEInstallDir[256];
Douglas Gregor80f93d92010-05-18 05:47:04 +0000345 char vsExpressIDEInstallDir[256];
Michael J. Spencerff58e362010-08-21 21:55:07 +0000346 // Then try the windows registry.
John Thompson75ee3bd2009-11-21 00:15:52 +0000347 bool hasVCDir = getSystemRegistryString(
348 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
349 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Douglas Gregor80f93d92010-05-18 05:47:04 +0000350 bool hasVCExpressDir = getSystemRegistryString(
351 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
352 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000353 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000354 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000355 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
356 if (p)
357 *p = '\0';
358 path = vsIDEInstallDir;
359 return(true);
360 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000361 else if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
362 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
363 if (p)
364 *p = '\0';
365 path = vsExpressIDEInstallDir;
366 return(true);
367 }
Mike Stump620d57a2009-10-12 20:50:45 +0000368 else {
369 // Try the environment.
Douglas Gregor80f93d92010-05-18 05:47:04 +0000370 const char* vs100comntools = getenv("VS100COMNTOOLS");
Mike Stump620d57a2009-10-12 20:50:45 +0000371 const char* vs90comntools = getenv("VS90COMNTOOLS");
372 const char* vs80comntools = getenv("VS80COMNTOOLS");
373 const char* vscomntools = NULL;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000374
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000375 // Try to find the version that we were compiled with
Douglas Gregor80f93d92010-05-18 05:47:04 +0000376 if(false) {}
377 #if (_MSC_VER >= 1600) // VC100
378 else if(vs100comntools) {
379 vscomntools = vs100comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000380 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000381 #elif (_MSC_VER == 1500) // VC80
382 else if(vs90comntools) {
383 vscomntools = vs90comntools;
384 }
385 #elif (_MSC_VER == 1400) // VC80
386 else if(vs80comntools) {
387 vscomntools = vs80comntools;
388 }
389 #endif
390 // Otherwise find any version we can
391 else if (vs100comntools)
392 vscomntools = vs100comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000393 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000394 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000395 else if (vs80comntools)
396 vscomntools = vs80comntools;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000397
Mike Stump620d57a2009-10-12 20:50:45 +0000398 if (vscomntools && *vscomntools) {
Dan Gohmancb421fa2010-04-19 16:39:44 +0000399 char *p = const_cast<char *>(strstr(vscomntools, "\\Common7\\Tools"));
Mike Stump620d57a2009-10-12 20:50:45 +0000400 if (p)
401 *p = '\0';
402 path = vscomntools;
403 return(true);
404 }
405 else
406 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000407 }
Mike Stump620d57a2009-10-12 20:50:45 +0000408 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000409}
Mike Stump43d81762009-10-08 23:29:47 +0000410
John Thompson75ee3bd2009-11-21 00:15:52 +0000411 // Get Windows SDK installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000412static bool getWindowsSDKDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000413 char windowsSDKInstallDir[256];
414 // Try the Windows registry.
415 bool hasSDKDir = getSystemRegistryString(
416 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
417 "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000418 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000419 if (hasSDKDir && windowsSDKInstallDir[0]) {
420 path = windowsSDKInstallDir;
421 return(true);
422 }
423 return(false);
424}
425
mike-m79bc57c2010-05-16 19:03:52 +0000426void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
427 const HeaderSearchOptions &HSOpts) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000428 llvm::Triple::OSType os = triple.getOS();
429
430 switch (os) {
431 case llvm::Triple::NetBSD:
432 break;
433 default:
434 // FIXME: temporary hack: hard-coded paths.
435 AddPath("/usr/local/include", System, true, false, false);
436 break;
437 }
mike-m79bc57c2010-05-16 19:03:52 +0000438
439 // Builtin includes use #include_next directives and should be positioned
440 // just prior C include dirs.
441 if (HSOpts.UseBuiltinIncludes) {
442 // Ignore the sys root, we *always* look for clang headers relative to
443 // supplied path.
444 llvm::sys::Path P(HSOpts.ResourceDir);
445 P.appendComponent("include");
446 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
447 }
448
449 // Add dirs specified via 'configure --with-c-include-dirs'.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000450 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
451 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000452 llvm::SmallVector<llvm::StringRef, 5> dirs;
453 CIncludeDirs.split(dirs, ":");
454 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
455 i != dirs.end();
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000456 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000457 AddPath(*i, System, false, false, false);
458 return;
459 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000460
Mike Stump43d81762009-10-08 23:29:47 +0000461 switch (os) {
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000462 case llvm::Triple::Win32: {
463 std::string VSDir;
464 std::string WindowsSDKDir;
465 if (getVisualStudioDir(VSDir)) {
466 AddPath(VSDir + "\\VC\\include", System, false, false, false);
467 if (getWindowsSDKDir(WindowsSDKDir))
468 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
469 else
470 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
471 System, false, false, false);
472 } else {
473 // Default install paths.
474 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
475 System, false, false, false);
476 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
477 System, false, false, false);
478 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000479 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000480 System, false, false, false);
481 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
482 System, false, false, false);
483 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000484 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000485 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000486 }
487 break;
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000488 }
Chris Lattner86ed3a32010-04-11 19:29:39 +0000489 case llvm::Triple::Haiku:
490 AddPath("/boot/common/include", System, true, false, false);
491 AddPath("/boot/develop/headers/os", System, true, false, false);
492 AddPath("/boot/develop/headers/os/app", System, true, false, false);
493 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
494 AddPath("/boot/develop/headers/os/device", System, true, false, false);
495 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000496 AddPath("/boot/develop/headers/os/game", System, true, false, false);
Chris Lattner86ed3a32010-04-11 19:29:39 +0000497 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
498 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
499 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
500 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
501 AddPath("/boot/develop/headers/os/media", System, true, false, false);
502 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
503 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
504 AddPath("/boot/develop/headers/os/net", System, true, false, false);
505 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
506 AddPath("/boot/develop/headers/os/support", System, true, false, false);
507 AddPath("/boot/develop/headers/os/translation",
508 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000509 AddPath("/boot/develop/headers/os/add-ons/graphics",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000510 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000511 AddPath("/boot/develop/headers/os/add-ons/input_server",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000512 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000513 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000514 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000515 AddPath("/boot/develop/headers/os/add-ons/tracker",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000516 System, true, false, false);
517 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
518 System, true, false, false);
519 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
520 System, true, false, false);
521 AddPath("/boot/develop/headers/os/be_apps/Tracker",
522 System, true, false, false);
523 AddPath("/boot/develop/headers/cpp", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000524 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000525 System, true, false, false);
526 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
527 AddPath("/boot/develop/headers/bsd", System, true, false, false);
528 AddPath("/boot/develop/headers/glibc", System, true, false, false);
529 AddPath("/boot/develop/headers/posix", System, true, false, false);
530 AddPath("/boot/develop/headers", System, true, false, false);
Eli Friedmana7e68452010-08-22 01:00:03 +0000531 break;
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000532 case llvm::Triple::Cygwin:
533 AddPath("/usr/include/w32api", System, true, false, false);
534 break;
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;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000571 case llvm::Triple::MinGW32:
NAKAMURA Takumi05c699e2011-02-17 08:51:47 +0000572 // mingw-w64-20110207
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000573 AddPath("c:/MinGW/include/c++/4.5.3", CXXSystem, true, false, false);
574 AddPath("c:/MinGW/include/c++/4.5.3/x86_64-w64-mingw32", CXXSystem, true,
575 false, false);
576 AddPath("c:/MinGW/include/c++/4.5.3/backward", CXXSystem, true, false,
577 false);
NAKAMURA Takumi05c699e2011-02-17 08:51:47 +0000578 // mingw-w64-20101129
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000579 AddPath("c:/MinGW/include/c++/4.5.2", CXXSystem, true, false, false);
580 AddPath("c:/MinGW/include/c++/4.5.2/x86_64-w64-mingw32", CXXSystem, true,
581 false, false);
582 AddPath("c:/MinGW/include/c++/4.5.2/backward", CXXSystem, true, false,
583 false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000584 // Try gcc 4.5.0
585 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000586 // Try gcc 4.4.0
587 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
588 // Try gcc 4.3.0
589 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
590 break;
591 case llvm::Triple::Darwin:
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000592 switch (triple.getArch()) {
593 default: break;
594
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000595 case llvm::Triple::ppc:
Douglas Gregor582c3012010-05-29 01:15:12 +0000596 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000597 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000598 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor616d4362010-05-29 01:21:11 +0000599 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000600 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000601 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor582c3012010-05-29 01:15:12 +0000602 triple);
603 break;
604
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000605 case llvm::Triple::x86:
606 case llvm::Triple::x86_64:
607 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
608 "i686-apple-darwin10", "", "x86_64", triple);
609 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
610 "i686-apple-darwin8", "", "", triple);
611 break;
612
613 case llvm::Triple::arm:
614 case llvm::Triple::thumb:
615 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
616 "arm-apple-darwin10", "v7", "", triple);
617 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
618 "arm-apple-darwin10", "v6", "", triple);
619 break;
620 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000621 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000622 case llvm::Triple::DragonFly:
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000623 AddPath("/usr/include/c++/4.1", CXXSystem, true, false, false);
Chris Lattner7a7ca282010-01-09 05:41:14 +0000624 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000625 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000626 //===------------------------------------------------------------------===//
627 // Debian based distros.
628 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
629 //===------------------------------------------------------------------===//
Rafael Espindolac12bbe52011-02-15 21:44:06 +0000630 // Ubuntu 10.10 "Maverick Meerkat" -- gcc-4.4.5
Rafael Espindolaadafdba2011-02-15 21:16:43 +0000631 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
632 "i686-linux-gnu", "", "64", triple);
633 // The rest of 10.10 is the same as previous versions.
634
mike-mac78b7a2010-05-06 14:11:13 +0000635 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
636 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
637 // Debian 6.0 "squeeze" -- gcc-4.4.2
638 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
639 "x86_64-linux-gnu", "32", "", triple);
640 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
641 "i486-linux-gnu", "", "64", triple);
Nick Lewyckydb083552011-02-01 21:32:14 +0000642 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
643 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000644 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
645 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
646 // Debian 5.0 "lenny" -- gcc-4.3.2
647 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
648 "x86_64-linux-gnu", "32", "", triple);
649 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
650 "i486-linux-gnu", "", "64", triple);
Rafael Espindolae7d6c2c2010-06-04 14:28:10 +0000651 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
652 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000653 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
654 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
655 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
656 "x86_64-linux-gnu", "32", "", triple);
657 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
658 "i486-linux-gnu", "", "64", triple);
659 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
660 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
661 "x86_64-linux-gnu", "32", "", triple);
662 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
663 "i486-linux-gnu", "", "64", triple);
664
665 //===------------------------------------------------------------------===//
666 // Redhat based distros.
667 //===------------------------------------------------------------------===//
Rafael Espindola6638b3a2010-11-02 18:39:34 +0000668 // Fedora 14
669 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
670 "x86_64-redhat-linux", "32", "", triple);
671 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
672 "i686-redhat-linux", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000673 // Fedora 13
Chris Lattner4336e192010-05-29 01:01:38 +0000674 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
675 "x86_64-redhat-linux", "32", "", triple);
676 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
677 "i686-redhat-linux","", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000678 // Fedora 12
679 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
680 "x86_64-redhat-linux", "32", "", triple);
681 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
682 "i686-redhat-linux","", "", triple);
683 // Fedora 12 (pre-FEB-2010)
684 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
685 "x86_64-redhat-linux", "32", "", triple);
686 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
687 "i686-redhat-linux","", "", triple);
688 // Fedora 11
689 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
690 "x86_64-redhat-linux", "32", "", triple);
691 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
692 "i586-redhat-linux","", "", triple);
693 // Fedora 10
694 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
695 "x86_64-redhat-linux", "32", "", triple);
696 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
697 "i386-redhat-linux","", "", triple);
698 // Fedora 9
699 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
700 "x86_64-redhat-linux", "32", "", triple);
701 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
702 "i386-redhat-linux", "", "", triple);
703 // Fedora 8
704 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
705 "x86_64-redhat-linux", "", "", triple);
706 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
707 "i386-redhat-linux", "", "", triple);
708
709 //===------------------------------------------------------------------===//
710
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000711 // Exherbo (2010-01-25)
712 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000713 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000714 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000715 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000716
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000717 // openSUSE 11.1 32 bit
718 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000719 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000720 // openSUSE 11.1 64 bit
721 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000722 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000723 // openSUSE 11.2
724 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000725 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000726 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000727 "x86_64-suse-linux", "", "", triple);
Rafael Espindola9d2c0602010-11-25 18:51:59 +0000728
729 // openSUSE 11.4
730 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
731 "i586-suse-linux", "", "", triple);
732 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
733 "x86_64-suse-linux", "", "", triple);
734
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000735 // Arch Linux 2008-06-24
736 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000737 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000738 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000739 "x86_64-unknown-linux-gnu", "", "", triple);
Nuno Lopes0d155a52010-09-11 17:51:45 +0000740 // Gentoo x86 2010.0 stable
741 AddGnuCPlusPlusIncludePaths(
742 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
743 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000744 // Gentoo x86 2009.1 stable
745 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000746 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000747 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000748 // Gentoo x86 2009.0 stable
749 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000750 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000751 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000752 // Gentoo x86 2008.0 stable
753 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000754 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000755 "i686-pc-linux-gnu", "", "", triple);
Nick Lewycky66935342010-07-24 21:33:13 +0000756
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000757 // Gentoo amd64 gcc 4.4.5
758 AddGnuCPlusPlusIncludePaths(
759 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4",
760 "x86_64-pc-linux-gnu", "32", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000761 // Gentoo amd64 gcc 4.4.4
762 AddGnuCPlusPlusIncludePaths(
763 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
764 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000765 // Gentoo amd64 gcc 4.4.3
766 AddGnuCPlusPlusIncludePaths(
767 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
768 "x86_64-pc-linux-gnu", "32", "", triple);
769 // Gentoo amd64 gcc 4.3.2
770 AddGnuCPlusPlusIncludePaths(
771 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
772 "x86_64-pc-linux-gnu", "", "", triple);
773 // Gentoo amd64 stable
774 AddGnuCPlusPlusIncludePaths(
775 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
776 "i686-pc-linux-gnu", "", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000777
Nick Lewycky66935342010-07-24 21:33:13 +0000778 // Gentoo amd64 llvm-gcc trunk
779 AddGnuCPlusPlusIncludePaths(
780 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
781 "x86_64-pc-linux-gnu", "", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000782
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000783 break;
784 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000785 // FreeBSD 8.0
786 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000787 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000788 break;
Anton Korobeynikovab079412010-08-31 22:39:50 +0000789 case llvm::Triple::NetBSD:
790 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
791 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000792 case llvm::Triple::OpenBSD: {
793 std::string t = triple.getTriple();
794 if (t.substr(0, 6) == "x86_64")
795 t.replace(0, 6, "amd64");
796 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
797 t, "", "", triple);
798 break;
799 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000800 case llvm::Triple::Minix:
801 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
802 "", "", "", triple);
803 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000804 case llvm::Triple::Solaris:
805 // Solaris - Fall though..
806 case llvm::Triple::AuroraUX:
807 // AuroraUX
808 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000809 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000810 break;
811 default:
812 break;
813 }
814}
815
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000816void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000817 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000818 const HeaderSearchOptions &HSOpts) {
Daniel Dunbar80c26f42010-09-09 17:38:22 +0000819 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes) {
820 if (!HSOpts.CXXSystemIncludes.empty()) {
821 for (unsigned i = 0, e = HSOpts.CXXSystemIncludes.size(); i != e; ++i)
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000822 AddPath(HSOpts.CXXSystemIncludes[i], CXXSystem, true, false, false);
Daniel Dunbar80c26f42010-09-09 17:38:22 +0000823 } else
824 AddDefaultCPlusPlusIncludePaths(triple);
825 }
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000826
mike-m79bc57c2010-05-16 19:03:52 +0000827 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000828
829 // Add the default framework include paths on Darwin.
830 if (triple.getOS() == llvm::Triple::Darwin) {
831 AddPath("/System/Library/Frameworks", System, true, false, true);
832 AddPath("/Library/Frameworks", System, true, false, true);
833 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000834}
835
Nico Weber0fca0222008-08-22 09:25:22 +0000836/// RemoveDuplicates - If there are duplicate directory entries in the specified
837/// search list, remove the later (dead) ones.
838static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000839 unsigned First, bool Verbose) {
Nico Weber0fca0222008-08-22 09:25:22 +0000840 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
841 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
842 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000843 for (unsigned i = First; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000844 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Chris Lattner43eee072009-02-08 01:00:10 +0000846 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattner43eee072009-02-08 01:00:10 +0000848 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000849 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000850 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000851 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000852 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000853 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000854 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000855 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000856 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000857 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000858 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000859 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000860 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000861 }
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Chris Lattner30f05b52009-02-08 00:55:22 +0000863 // If we have a normal #include dir/framework/headermap that is shadowed
864 // later in the chain by a system include location, we actually want to
865 // ignore the user's request and drop the user dir... keeping the system
866 // dir. This is weird, but required to emulate GCC's search path correctly.
867 //
868 // Since dupes of system dirs are rare, just rescan to find the original
869 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000870 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000871 // Find the dir that this is the same of.
872 unsigned FirstDir;
873 for (FirstDir = 0; ; ++FirstDir) {
874 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Chris Lattner43eee072009-02-08 01:00:10 +0000876 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
877
Chris Lattner30f05b52009-02-08 00:55:22 +0000878 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000879 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000880 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattner30f05b52009-02-08 00:55:22 +0000882 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000883 if (CurEntry.isNormalDir())
884 isSame = SearchEntry.getDir() == CurEntry.getDir();
885 else if (CurEntry.isFramework())
886 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000887 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000888 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
889 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000890 }
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Chris Lattner30f05b52009-02-08 00:55:22 +0000892 if (isSame)
893 break;
894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Chris Lattner30f05b52009-02-08 00:55:22 +0000896 // If the first dir in the search path is a non-system dir, zap it
897 // instead of the system one.
898 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
899 DirToRemove = FirstDir;
900 }
901
902 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000903 llvm::errs() << "ignoring duplicate directory \""
904 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +0000905 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000906 llvm::errs() << " as it is a non-system directory that duplicates "
907 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000908 }
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Chris Lattner7a739402008-09-26 17:46:45 +0000910 // This is reached if the current entry is a duplicate. Remove the
911 // DirToRemove (usually the current dir).
912 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000913 --i;
914 }
915}
916
917
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000918void InitHeaderSearch::Realize(const LangOptions &Lang) {
Nico Weber0fca0222008-08-22 09:25:22 +0000919 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
920 std::vector<DirectoryLookup> SearchList;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000921 SearchList.reserve(IncludePath.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000923 /* Quoted arguments go first. */
924 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
925 it != ie; ++it) {
926 if (it->first == Quoted)
927 SearchList.push_back(it->second);
928 }
929 /* Deduplicate and remember index */
930 RemoveDuplicates(SearchList, 0, Verbose);
931 unsigned quoted = SearchList.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000933 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
934 it != ie; ++it) {
935 if (it->first == Angled)
936 SearchList.push_back(it->second);
937 }
938
939 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
940 it != ie; ++it) {
941 if (it->first == System || (Lang.CPlusPlus && it->first == CXXSystem))
942 SearchList.push_back(it->second);
943 }
944
945 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
946 it != ie; ++it) {
947 if (it->first == After)
948 SearchList.push_back(it->second);
949 }
950
951 RemoveDuplicates(SearchList, quoted, Verbose);
Nico Weber0fca0222008-08-22 09:25:22 +0000952
953 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000954 Headers.SetSearchPaths(SearchList, quoted, DontSearchCurDir);
Nico Weber0fca0222008-08-22 09:25:22 +0000955
956 // If verbose, print the list of directories that will be searched.
957 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000958 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000959 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000960 if (i == quoted)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000961 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000962 const char *Name = SearchList[i].getName();
963 const char *Suffix;
964 if (SearchList[i].isNormalDir())
965 Suffix = "";
966 else if (SearchList[i].isFramework())
967 Suffix = " (framework directory)";
968 else {
969 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
970 Suffix = " (headermap)";
971 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000972 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000973 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000974 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000975 }
976}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000977
Daniel Dunbar5814e652009-11-11 21:44:21 +0000978void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
979 const HeaderSearchOptions &HSOpts,
980 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000981 const llvm::Triple &Triple) {
982 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
983
984 // Add the user defined entries.
985 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
986 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +0000987 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
Chris Lattner23637be2010-08-24 22:27:37 +0000988 !E.IsSysRootRelative);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000989 }
990
991 // Add entries from CPATH and friends.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000992 Init.AddDelimitedPaths(HSOpts.EnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000993 if (Lang.CPlusPlus && Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000994 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000995 else if (Lang.CPlusPlus)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000996 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000997 else if (Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000998 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000999 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +00001000 Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001001
Daniel Dunbardd35ce92009-11-07 04:58:12 +00001002 if (HSOpts.UseStandardIncludes)
mike-m79bc57c2010-05-16 19:03:52 +00001003 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001004
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001005 Init.Realize(Lang);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001006}