blob: 2a27fb1bcafb3fde9b2173e3be7f085dc91ebf11 [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) {
Roman Divacky63076602011-03-01 18:08:03 +0000431 case llvm::Triple::FreeBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000432 case llvm::Triple::NetBSD:
433 break;
434 default:
435 // FIXME: temporary hack: hard-coded paths.
436 AddPath("/usr/local/include", System, true, false, false);
437 break;
438 }
mike-m79bc57c2010-05-16 19:03:52 +0000439
440 // Builtin includes use #include_next directives and should be positioned
441 // just prior C include dirs.
442 if (HSOpts.UseBuiltinIncludes) {
443 // Ignore the sys root, we *always* look for clang headers relative to
444 // supplied path.
445 llvm::sys::Path P(HSOpts.ResourceDir);
446 P.appendComponent("include");
447 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
448 }
449
450 // Add dirs specified via 'configure --with-c-include-dirs'.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000451 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
452 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000453 llvm::SmallVector<llvm::StringRef, 5> dirs;
454 CIncludeDirs.split(dirs, ":");
455 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
456 i != dirs.end();
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000457 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000458 AddPath(*i, System, false, false, false);
459 return;
460 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000461
Mike Stump43d81762009-10-08 23:29:47 +0000462 switch (os) {
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000463 case llvm::Triple::Win32: {
464 std::string VSDir;
465 std::string WindowsSDKDir;
466 if (getVisualStudioDir(VSDir)) {
467 AddPath(VSDir + "\\VC\\include", System, false, false, false);
468 if (getWindowsSDKDir(WindowsSDKDir))
469 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
470 else
471 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
472 System, false, false, false);
473 } else {
474 // Default install paths.
475 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
476 System, false, false, false);
477 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
478 System, false, false, false);
479 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000480 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000481 System, false, false, false);
482 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
483 System, false, false, false);
484 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000485 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000486 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000487 }
488 break;
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000489 }
Chris Lattner86ed3a32010-04-11 19:29:39 +0000490 case llvm::Triple::Haiku:
491 AddPath("/boot/common/include", System, true, false, false);
492 AddPath("/boot/develop/headers/os", System, true, false, false);
493 AddPath("/boot/develop/headers/os/app", System, true, false, false);
494 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
495 AddPath("/boot/develop/headers/os/device", System, true, false, false);
496 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000497 AddPath("/boot/develop/headers/os/game", System, true, false, false);
Chris Lattner86ed3a32010-04-11 19:29:39 +0000498 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
499 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
500 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
501 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
502 AddPath("/boot/develop/headers/os/media", System, true, false, false);
503 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
504 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
505 AddPath("/boot/develop/headers/os/net", System, true, false, false);
506 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
507 AddPath("/boot/develop/headers/os/support", System, true, false, false);
508 AddPath("/boot/develop/headers/os/translation",
509 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000510 AddPath("/boot/develop/headers/os/add-ons/graphics",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000511 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000512 AddPath("/boot/develop/headers/os/add-ons/input_server",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000513 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000514 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000515 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000516 AddPath("/boot/develop/headers/os/add-ons/tracker",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000517 System, true, false, false);
518 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
519 System, true, false, false);
520 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
521 System, true, false, false);
522 AddPath("/boot/develop/headers/os/be_apps/Tracker",
523 System, true, false, false);
524 AddPath("/boot/develop/headers/cpp", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000525 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000526 System, true, false, false);
527 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
528 AddPath("/boot/develop/headers/bsd", System, true, false, false);
529 AddPath("/boot/develop/headers/glibc", System, true, false, false);
530 AddPath("/boot/develop/headers/posix", System, true, false, false);
531 AddPath("/boot/develop/headers", System, true, false, false);
Eli Friedmana7e68452010-08-22 01:00:03 +0000532 break;
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000533 case llvm::Triple::Cygwin:
534 AddPath("/usr/include/w32api", System, true, false, false);
535 break;
Mike Stump620d57a2009-10-12 20:50:45 +0000536 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000537 AddPath("c:/mingw/include", System, true, false, false);
538 break;
539 default:
Mike Stump43d81762009-10-08 23:29:47 +0000540 break;
541 }
John Thompsond3f88342009-10-13 18:51:32 +0000542
John Thompsond3f88342009-10-13 18:51:32 +0000543 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000544}
545
Chris Lattner0e3cc052010-05-05 05:28:39 +0000546void InitHeaderSearch::
547AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000548 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000549 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
550 if (CxxIncludeRoot != "") {
551 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
552 if (CxxIncludeArch == "")
553 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
Chris Lattner0e3cc052010-05-05 05:28:39 +0000554 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
555 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000556 else
557 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
Chris Lattner0e3cc052010-05-05 05:28:39 +0000558 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
559 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000560 return;
561 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000562 // FIXME: temporary hack: hard-coded paths.
563 switch (os) {
564 case llvm::Triple::Cygwin:
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000565 // Cygwin-1.7
566 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
567 // g++-4 / Cygwin-1.5
568 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
569 // FIXME: Do we support g++-3.4.4?
570 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000571 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000572 case llvm::Triple::MinGW32:
NAKAMURA Takumi05c699e2011-02-17 08:51:47 +0000573 // mingw-w64-20110207
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000574 AddPath("c:/MinGW/include/c++/4.5.3", CXXSystem, true, false, false);
575 AddPath("c:/MinGW/include/c++/4.5.3/x86_64-w64-mingw32", CXXSystem, true,
576 false, false);
577 AddPath("c:/MinGW/include/c++/4.5.3/backward", CXXSystem, true, false,
578 false);
NAKAMURA Takumi05c699e2011-02-17 08:51:47 +0000579 // mingw-w64-20101129
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000580 AddPath("c:/MinGW/include/c++/4.5.2", CXXSystem, true, false, false);
581 AddPath("c:/MinGW/include/c++/4.5.2/x86_64-w64-mingw32", CXXSystem, true,
582 false, false);
583 AddPath("c:/MinGW/include/c++/4.5.2/backward", CXXSystem, true, false,
584 false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000585 // Try gcc 4.5.0
586 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000587 // Try gcc 4.4.0
588 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
589 // Try gcc 4.3.0
590 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
591 break;
592 case llvm::Triple::Darwin:
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000593 switch (triple.getArch()) {
594 default: break;
595
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000596 case llvm::Triple::ppc:
Douglas Gregor582c3012010-05-29 01:15:12 +0000597 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000598 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000599 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor616d4362010-05-29 01:21:11 +0000600 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000601 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000602 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor582c3012010-05-29 01:15:12 +0000603 triple);
604 break;
605
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000606 case llvm::Triple::x86:
607 case llvm::Triple::x86_64:
608 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
609 "i686-apple-darwin10", "", "x86_64", triple);
610 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
611 "i686-apple-darwin8", "", "", triple);
612 break;
613
614 case llvm::Triple::arm:
615 case llvm::Triple::thumb:
616 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
617 "arm-apple-darwin10", "v7", "", triple);
618 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
619 "arm-apple-darwin10", "v6", "", triple);
620 break;
621 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000622 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000623 case llvm::Triple::DragonFly:
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000624 AddPath("/usr/include/c++/4.1", CXXSystem, true, false, false);
Chris Lattner7a7ca282010-01-09 05:41:14 +0000625 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000626 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000627 //===------------------------------------------------------------------===//
628 // Debian based distros.
629 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
630 //===------------------------------------------------------------------===//
Rafael Espindolac12bbe52011-02-15 21:44:06 +0000631 // Ubuntu 10.10 "Maverick Meerkat" -- gcc-4.4.5
Rafael Espindolaadafdba2011-02-15 21:16:43 +0000632 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
633 "i686-linux-gnu", "", "64", triple);
634 // The rest of 10.10 is the same as previous versions.
635
mike-mac78b7a2010-05-06 14:11:13 +0000636 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
637 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
638 // Debian 6.0 "squeeze" -- gcc-4.4.2
639 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
640 "x86_64-linux-gnu", "32", "", triple);
641 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
642 "i486-linux-gnu", "", "64", triple);
Nick Lewyckydb083552011-02-01 21:32:14 +0000643 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
644 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000645 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
646 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
647 // Debian 5.0 "lenny" -- gcc-4.3.2
648 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
649 "x86_64-linux-gnu", "32", "", triple);
650 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
651 "i486-linux-gnu", "", "64", triple);
Rafael Espindolae7d6c2c2010-06-04 14:28:10 +0000652 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
653 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000654 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
655 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
656 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
657 "x86_64-linux-gnu", "32", "", triple);
658 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
659 "i486-linux-gnu", "", "64", triple);
660 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
661 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
662 "x86_64-linux-gnu", "32", "", triple);
663 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
664 "i486-linux-gnu", "", "64", triple);
665
666 //===------------------------------------------------------------------===//
667 // Redhat based distros.
668 //===------------------------------------------------------------------===//
Rafael Espindola6638b3a2010-11-02 18:39:34 +0000669 // Fedora 14
670 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
671 "x86_64-redhat-linux", "32", "", triple);
672 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
673 "i686-redhat-linux", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000674 // Fedora 13
Chris Lattner4336e192010-05-29 01:01:38 +0000675 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
676 "x86_64-redhat-linux", "32", "", triple);
677 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
678 "i686-redhat-linux","", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000679 // Fedora 12
680 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
681 "x86_64-redhat-linux", "32", "", triple);
682 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
683 "i686-redhat-linux","", "", triple);
684 // Fedora 12 (pre-FEB-2010)
685 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
686 "x86_64-redhat-linux", "32", "", triple);
687 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
688 "i686-redhat-linux","", "", triple);
689 // Fedora 11
690 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
691 "x86_64-redhat-linux", "32", "", triple);
692 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
693 "i586-redhat-linux","", "", triple);
694 // Fedora 10
695 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
696 "x86_64-redhat-linux", "32", "", triple);
697 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
698 "i386-redhat-linux","", "", triple);
699 // Fedora 9
700 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
701 "x86_64-redhat-linux", "32", "", triple);
702 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
703 "i386-redhat-linux", "", "", triple);
704 // Fedora 8
705 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
706 "x86_64-redhat-linux", "", "", triple);
707 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
708 "i386-redhat-linux", "", "", triple);
709
710 //===------------------------------------------------------------------===//
711
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000712 // Exherbo (2010-01-25)
713 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000714 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000715 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000716 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000717
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000718 // openSUSE 11.1 32 bit
719 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000720 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000721 // openSUSE 11.1 64 bit
722 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000723 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000724 // openSUSE 11.2
725 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000726 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000727 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000728 "x86_64-suse-linux", "", "", triple);
Rafael Espindola9d2c0602010-11-25 18:51:59 +0000729
730 // openSUSE 11.4
731 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
732 "i586-suse-linux", "", "", triple);
733 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
734 "x86_64-suse-linux", "", "", triple);
735
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000736 // Arch Linux 2008-06-24
737 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000738 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000739 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000740 "x86_64-unknown-linux-gnu", "", "", triple);
Nuno Lopes0d155a52010-09-11 17:51:45 +0000741 // Gentoo x86 2010.0 stable
742 AddGnuCPlusPlusIncludePaths(
743 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
744 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000745 // Gentoo x86 2009.1 stable
746 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000747 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000748 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000749 // Gentoo x86 2009.0 stable
750 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000751 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000752 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000753 // Gentoo x86 2008.0 stable
754 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000755 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000756 "i686-pc-linux-gnu", "", "", triple);
Nick Lewycky66935342010-07-24 21:33:13 +0000757
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000758 // Gentoo amd64 gcc 4.4.5
759 AddGnuCPlusPlusIncludePaths(
760 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4",
761 "x86_64-pc-linux-gnu", "32", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000762 // Gentoo amd64 gcc 4.4.4
763 AddGnuCPlusPlusIncludePaths(
764 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
765 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000766 // Gentoo amd64 gcc 4.4.3
767 AddGnuCPlusPlusIncludePaths(
768 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
769 "x86_64-pc-linux-gnu", "32", "", triple);
770 // Gentoo amd64 gcc 4.3.2
771 AddGnuCPlusPlusIncludePaths(
772 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
773 "x86_64-pc-linux-gnu", "", "", triple);
774 // Gentoo amd64 stable
775 AddGnuCPlusPlusIncludePaths(
776 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
777 "i686-pc-linux-gnu", "", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000778
Nick Lewycky66935342010-07-24 21:33:13 +0000779 // Gentoo amd64 llvm-gcc trunk
780 AddGnuCPlusPlusIncludePaths(
781 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
782 "x86_64-pc-linux-gnu", "", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000783
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000784 break;
785 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000786 // FreeBSD 8.0
787 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000788 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000789 break;
Anton Korobeynikovab079412010-08-31 22:39:50 +0000790 case llvm::Triple::NetBSD:
791 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
792 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000793 case llvm::Triple::OpenBSD: {
794 std::string t = triple.getTriple();
795 if (t.substr(0, 6) == "x86_64")
796 t.replace(0, 6, "amd64");
797 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
798 t, "", "", triple);
799 break;
800 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000801 case llvm::Triple::Minix:
802 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
803 "", "", "", triple);
804 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000805 case llvm::Triple::Solaris:
806 // Solaris - Fall though..
807 case llvm::Triple::AuroraUX:
808 // AuroraUX
809 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000810 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000811 break;
812 default:
813 break;
814 }
815}
816
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000817void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000818 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000819 const HeaderSearchOptions &HSOpts) {
Joerg Sonnenberger0bb208c2011-02-22 15:19:35 +0000820 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes)
821 AddDefaultCPlusPlusIncludePaths(triple);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000822
mike-m79bc57c2010-05-16 19:03:52 +0000823 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000824
825 // Add the default framework include paths on Darwin.
826 if (triple.getOS() == llvm::Triple::Darwin) {
827 AddPath("/System/Library/Frameworks", System, true, false, true);
828 AddPath("/Library/Frameworks", System, true, false, true);
829 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000830}
831
Nico Weber0fca0222008-08-22 09:25:22 +0000832/// RemoveDuplicates - If there are duplicate directory entries in the specified
833/// search list, remove the later (dead) ones.
834static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000835 unsigned First, bool Verbose) {
Nico Weber0fca0222008-08-22 09:25:22 +0000836 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
837 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
838 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000839 for (unsigned i = First; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000840 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Chris Lattner43eee072009-02-08 01:00:10 +0000842 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Chris Lattner43eee072009-02-08 01:00:10 +0000844 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000845 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000846 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000847 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000848 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000849 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000850 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000851 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000852 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000853 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000854 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000855 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000856 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000857 }
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Chris Lattner30f05b52009-02-08 00:55:22 +0000859 // If we have a normal #include dir/framework/headermap that is shadowed
860 // later in the chain by a system include location, we actually want to
861 // ignore the user's request and drop the user dir... keeping the system
862 // dir. This is weird, but required to emulate GCC's search path correctly.
863 //
864 // Since dupes of system dirs are rare, just rescan to find the original
865 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000866 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000867 // Find the dir that this is the same of.
868 unsigned FirstDir;
869 for (FirstDir = 0; ; ++FirstDir) {
870 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Chris Lattner43eee072009-02-08 01:00:10 +0000872 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
873
Chris Lattner30f05b52009-02-08 00:55:22 +0000874 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000875 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000876 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Chris Lattner30f05b52009-02-08 00:55:22 +0000878 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000879 if (CurEntry.isNormalDir())
880 isSame = SearchEntry.getDir() == CurEntry.getDir();
881 else if (CurEntry.isFramework())
882 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000883 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000884 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
885 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Chris Lattner30f05b52009-02-08 00:55:22 +0000888 if (isSame)
889 break;
890 }
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Chris Lattner30f05b52009-02-08 00:55:22 +0000892 // If the first dir in the search path is a non-system dir, zap it
893 // instead of the system one.
894 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
895 DirToRemove = FirstDir;
896 }
897
898 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000899 llvm::errs() << "ignoring duplicate directory \""
900 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +0000901 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000902 llvm::errs() << " as it is a non-system directory that duplicates "
903 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000904 }
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Chris Lattner7a739402008-09-26 17:46:45 +0000906 // This is reached if the current entry is a duplicate. Remove the
907 // DirToRemove (usually the current dir).
908 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000909 --i;
910 }
911}
912
913
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000914void InitHeaderSearch::Realize(const LangOptions &Lang) {
Nico Weber0fca0222008-08-22 09:25:22 +0000915 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
916 std::vector<DirectoryLookup> SearchList;
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000917 SearchList.reserve(IncludePath.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000919 /* Quoted arguments go first. */
920 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
921 it != ie; ++it) {
922 if (it->first == Quoted)
923 SearchList.push_back(it->second);
924 }
925 /* Deduplicate and remember index */
926 RemoveDuplicates(SearchList, 0, Verbose);
927 unsigned quoted = SearchList.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000929 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
930 it != ie; ++it) {
931 if (it->first == Angled)
932 SearchList.push_back(it->second);
933 }
934
935 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
936 it != ie; ++it) {
937 if (it->first == System || (Lang.CPlusPlus && it->first == CXXSystem))
938 SearchList.push_back(it->second);
939 }
940
941 for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
942 it != ie; ++it) {
943 if (it->first == After)
944 SearchList.push_back(it->second);
945 }
946
947 RemoveDuplicates(SearchList, quoted, Verbose);
Nico Weber0fca0222008-08-22 09:25:22 +0000948
949 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000950 Headers.SetSearchPaths(SearchList, quoted, DontSearchCurDir);
Nico Weber0fca0222008-08-22 09:25:22 +0000951
952 // If verbose, print the list of directories that will be searched.
953 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000954 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000955 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000956 if (i == quoted)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000957 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000958 const char *Name = SearchList[i].getName();
959 const char *Suffix;
960 if (SearchList[i].isNormalDir())
961 Suffix = "";
962 else if (SearchList[i].isFramework())
963 Suffix = " (framework directory)";
964 else {
965 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
966 Suffix = " (headermap)";
967 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000968 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000969 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000970 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000971 }
972}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000973
Daniel Dunbar5814e652009-11-11 21:44:21 +0000974void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
975 const HeaderSearchOptions &HSOpts,
976 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000977 const llvm::Triple &Triple) {
978 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
979
980 // Add the user defined entries.
981 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
982 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +0000983 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
Chris Lattner23637be2010-08-24 22:27:37 +0000984 !E.IsSysRootRelative);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000985 }
986
987 // Add entries from CPATH and friends.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000988 Init.AddDelimitedPaths(HSOpts.EnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000989 if (Lang.CPlusPlus && Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000990 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000991 else if (Lang.CPlusPlus)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000992 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000993 else if (Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000994 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000995 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000996 Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000997
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000998 if (HSOpts.UseStandardIncludes)
mike-m79bc57c2010-05-16 19:03:52 +0000999 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001000
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001001 Init.Realize(Lang);
Daniel Dunbar63c8b772009-11-07 04:20:50 +00001002}