blob: 4855b62b747d340bbf9c6cced5dda8be8adb16cd [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 {
45 std::vector<DirectoryLookup> IncludeGroup[4];
46 HeaderSearch& Headers;
47 bool Verbose;
Michael J. Spenceraf6530c2010-12-25 20:09:27 +000048 std::string IncludeSysroot;
49 bool IsNotEmptyOrRoot;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000050
51public:
52
Chandler Carruthc09265a2010-11-15 07:15:26 +000053 InitHeaderSearch(HeaderSearch &HS, bool verbose, llvm::StringRef sysroot)
Michael J. Spenceraf6530c2010-12-25 20:09:27 +000054 : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
55 IsNotEmptyOrRoot(!(sysroot.empty() || sysroot == "/")) {
Chandler Carruthc09265a2010-11-15 07:15:26 +000056 }
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000057
58 /// AddPath - Add the specified path to the specified group list.
Benjamin Kramere89ba592009-12-08 12:38:20 +000059 void AddPath(const llvm::Twine &Path, IncludeDirGroup Group,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000060 bool isCXXAware, bool isUserSupplied,
61 bool isFramework, bool IgnoreSysRoot = false);
62
mike-ma6087372010-05-05 17:00:31 +000063 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000064 /// libstdc++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000065 void AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
66 llvm::StringRef ArchDir,
67 llvm::StringRef Dir32,
68 llvm::StringRef Dir64,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000069 const llvm::Triple &triple);
70
Michael J. Spencer06a8dc62010-12-21 16:45:42 +000071 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000072 /// libstdc++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000073 void AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
74 llvm::StringRef Arch,
75 llvm::StringRef Version);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000076
77 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
78 /// separator. The processing follows that of the CPATH variable for gcc.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +000079 void AddDelimitedPaths(llvm::StringRef String);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000080
81 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-m79bc57c2010-05-16 19:03:52 +000082 void AddDefaultCIncludePaths(const llvm::Triple &triple,
83 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000084
85 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
86 // compiling c++.
87 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
88
89 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
90 /// that e.g. stdio.h is found.
91 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +000092 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +000093 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000094
95 /// Realize - Merges all search path lists into one list and send it to
96 /// HeaderSearch.
97 void Realize();
98};
99
100}
Nico Weber0fca0222008-08-22 09:25:22 +0000101
Benjamin Kramere89ba592009-12-08 12:38:20 +0000102void InitHeaderSearch::AddPath(const llvm::Twine &Path,
Benjamin Kramer458fb102009-09-05 09:49:39 +0000103 IncludeDirGroup Group, bool isCXXAware,
104 bool isUserSupplied, bool isFramework,
105 bool IgnoreSysRoot) {
Benjamin Kramere89ba592009-12-08 12:38:20 +0000106 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
Nico Weber0fca0222008-08-22 09:25:22 +0000107 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Nico Weber0fca0222008-08-22 09:25:22 +0000109 // Compute the actual path, taking into consideration -isysroot.
Chandler Carruth5853b0f2010-11-15 00:05:18 +0000110 llvm::SmallString<256> MappedPathStorage;
Chandler Carruthf3721452010-11-15 00:48:13 +0000111 llvm::StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Nico Weber0fca0222008-08-22 09:25:22 +0000113 // Handle isysroot.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000114 if (Group == System && !IgnoreSysRoot &&
115 llvm::sys::path::is_absolute(MappedPathStr) &&
Michael J. Spenceraf6530c2010-12-25 20:09:27 +0000116 IsNotEmptyOrRoot) {
Chandler Carruth5619ae52010-11-15 09:28:23 +0000117 MappedPathStorage.clear();
Chandler Carruthc09265a2010-11-15 07:15:26 +0000118 MappedPathStr =
Michael J. Spenceraf6530c2010-12-25 20:09:27 +0000119 (IncludeSysroot + Path).toStringRef(MappedPathStorage);
Nico Weber0fca0222008-08-22 09:25:22 +0000120 }
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Nico Weber0fca0222008-08-22 09:25:22 +0000122 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000123 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000124 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000125 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000126 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000127 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000128 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000129 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000130
131
Nico Weber0fca0222008-08-22 09:25:22 +0000132 // If the directory exists, add it.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000133 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000134 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
135 isFramework));
136 return;
137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Nico Weber0fca0222008-08-22 09:25:22 +0000139 // Check to see if this is an apple-style headermap (which are not allowed to
140 // be frameworks).
141 if (!isFramework) {
Chris Lattner39b49bc2010-11-23 08:35:12 +0000142 if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
Nico Weber0fca0222008-08-22 09:25:22 +0000143 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
144 // It is a headermap, add it to the search path.
145 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
146 return;
147 }
148 }
149 }
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Nico Weber0fca0222008-08-22 09:25:22 +0000151 if (Verbose)
Chandler Carruthf3721452010-11-15 00:48:13 +0000152 llvm::errs() << "ignoring nonexistent directory \""
153 << MappedPathStr << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000154}
155
156
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000157void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) {
158 if (at.empty()) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000159 return;
160
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000161 llvm::StringRef::size_type delim;
162 while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) {
163 if (delim == 0)
Nico Weber0fca0222008-08-22 09:25:22 +0000164 AddPath(".", Angled, false, true, false);
165 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000166 AddPath(at.substr(0, delim), Angled, false, true, false);
167 at = at.substr(delim + 1);
Nico Weber0fca0222008-08-22 09:25:22 +0000168 }
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000169
170 if (at.empty())
Nico Weber0fca0222008-08-22 09:25:22 +0000171 AddPath(".", Angled, false, true, false);
172 else
173 AddPath(at, Angled, false, true, false);
174}
175
Benjamin Kramere89ba592009-12-08 12:38:20 +0000176void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
177 llvm::StringRef ArchDir,
178 llvm::StringRef Dir32,
179 llvm::StringRef Dir64,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000180 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000181 // Add the base dir
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000182 AddPath(Base, System, true, false, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000183
184 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000185 llvm::Triple::ArchType arch = triple.getArch();
186 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000187 if (is64bit)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000188 AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000189 else
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000190 AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000191
192 // Add the backward dir
193 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000194}
Nico Weber0fca0222008-08-22 09:25:22 +0000195
Benjamin Kramere89ba592009-12-08 12:38:20 +0000196void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
197 llvm::StringRef Arch,
198 llvm::StringRef Version) {
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000199 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
200 System, true, false, false);
Chris Lattner8e9006b2010-09-03 16:45:53 +0000201 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
202 System, true, false, false);
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000203 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
204 System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000205}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000206
Mike Stump620d57a2009-10-12 20:50:45 +0000207 // FIXME: This probably should goto to some platform utils place.
208#ifdef _MSC_VER
John Thompson75ee3bd2009-11-21 00:15:52 +0000209
Mike Stump620d57a2009-10-12 20:50:45 +0000210 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000211 // This also supports a means to look for high-versioned keys by use
212 // of a $VERSION placeholder in the key path.
213 // $VERSION in the key path is a placeholder for the version number,
214 // causing the highest value path to be searched for and used.
215 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
216 // There can be additional characters in the component. Only the numberic
217 // characters are compared.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000218static bool getSystemRegistryString(const char *keyPath, const char *valueName,
219 char *value, size_t maxLength) {
Mike Stump43d81762009-10-08 23:29:47 +0000220 HKEY hRootKey = NULL;
221 HKEY hKey = NULL;
222 const char* subKey = NULL;
223 DWORD valueType;
224 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000225 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000226 bool returnValue = false;
227 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
228 hRootKey = HKEY_CLASSES_ROOT;
229 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000230 }
Mike Stump43d81762009-10-08 23:29:47 +0000231 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
232 hRootKey = HKEY_USERS;
233 subKey = keyPath + 11;
234 }
235 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
236 hRootKey = HKEY_LOCAL_MACHINE;
237 subKey = keyPath + 19;
238 }
239 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
240 hRootKey = HKEY_CURRENT_USER;
241 subKey = keyPath + 18;
242 }
243 else
244 return(false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000245 const char *placeHolder = strstr(subKey, "$VERSION");
246 char bestName[256];
247 bestName[0] = '\0';
248 // If we have a $VERSION placeholder, do the highest-version search.
249 if (placeHolder) {
250 const char *keyEnd = placeHolder - 1;
251 const char *nextKey = placeHolder;
252 // Find end of previous key.
253 while ((keyEnd > subKey) && (*keyEnd != '\\'))
254 keyEnd--;
255 // Find end of key containing $VERSION.
256 while (*nextKey && (*nextKey != '\\'))
257 nextKey++;
258 size_t partialKeyLength = keyEnd - subKey;
259 char partialKey[256];
260 if (partialKeyLength > sizeof(partialKey))
261 partialKeyLength = sizeof(partialKey);
262 strncpy(partialKey, subKey, partialKeyLength);
263 partialKey[partialKeyLength] = '\0';
264 HKEY hTopKey = NULL;
265 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
266 if (lResult == ERROR_SUCCESS) {
267 char keyName[256];
268 int bestIndex = -1;
269 double bestValue = 0.0;
270 DWORD index, size = sizeof(keyName) - 1;
Nuno Lopes33cc2432009-12-07 17:18:48 +0000271 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
272 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
273 const char *sp = keyName;
274 while (*sp && !isdigit(*sp))
275 sp++;
276 if (!*sp)
277 continue;
278 const char *ep = sp + 1;
279 while (*ep && (isdigit(*ep) || (*ep == '.')))
280 ep++;
281 char numBuf[32];
282 strncpy(numBuf, sp, sizeof(numBuf) - 1);
283 numBuf[sizeof(numBuf) - 1] = '\0';
284 double value = strtod(numBuf, NULL);
285 if (value > bestValue) {
286 bestIndex = (int)index;
287 bestValue = value;
288 strcpy(bestName, keyName);
289 }
John Thompson75ee3bd2009-11-21 00:15:52 +0000290 size = sizeof(keyName) - 1;
291 }
292 // If we found the highest versioned key, open the key and get the value.
293 if (bestIndex != -1) {
294 // Append rest of key.
295 strncat(bestName, nextKey, sizeof(bestName) - 1);
296 bestName[sizeof(bestName) - 1] = '\0';
297 // Open the chosen key path remainder.
298 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
299 if (lResult == ERROR_SUCCESS) {
300 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
301 (LPBYTE)value, &valueSize);
302 if (lResult == ERROR_SUCCESS)
303 returnValue = true;
304 RegCloseKey(hKey);
305 }
306 }
307 RegCloseKey(hTopKey);
308 }
309 }
310 else {
311 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
312 if (lResult == ERROR_SUCCESS) {
313 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
314 (LPBYTE)value, &valueSize);
315 if (lResult == ERROR_SUCCESS)
316 returnValue = true;
317 RegCloseKey(hKey);
318 }
Mike Stump43d81762009-10-08 23:29:47 +0000319 }
320 return(returnValue);
321}
Mike Stump620d57a2009-10-12 20:50:45 +0000322#else // _MSC_VER
323 // Read registry string.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000324static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
Mike Stump620d57a2009-10-12 20:50:45 +0000325 return(false);
326}
327#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000328
Mike Stump43d81762009-10-08 23:29:47 +0000329 // Get Visual Studio installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000330static bool getVisualStudioDir(std::string &path) {
Michael J. Spencerff58e362010-08-21 21:55:07 +0000331 // First check the environment variables that vsvars32.bat sets.
332 const char* vcinstalldir = getenv("VCINSTALLDIR");
333 if(vcinstalldir) {
334 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
335 if (p)
336 *p = '\0';
337 path = vcinstalldir;
338 return(true);
339 }
340
John Thompson75ee3bd2009-11-21 00:15:52 +0000341 char vsIDEInstallDir[256];
Douglas Gregor80f93d92010-05-18 05:47:04 +0000342 char vsExpressIDEInstallDir[256];
Michael J. Spencerff58e362010-08-21 21:55:07 +0000343 // Then try the windows registry.
John Thompson75ee3bd2009-11-21 00:15:52 +0000344 bool hasVCDir = getSystemRegistryString(
345 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
346 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Douglas Gregor80f93d92010-05-18 05:47:04 +0000347 bool hasVCExpressDir = getSystemRegistryString(
348 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
349 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000350 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000351 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000352 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
353 if (p)
354 *p = '\0';
355 path = vsIDEInstallDir;
356 return(true);
357 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000358 else if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
359 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
360 if (p)
361 *p = '\0';
362 path = vsExpressIDEInstallDir;
363 return(true);
364 }
Mike Stump620d57a2009-10-12 20:50:45 +0000365 else {
366 // Try the environment.
Douglas Gregor80f93d92010-05-18 05:47:04 +0000367 const char* vs100comntools = getenv("VS100COMNTOOLS");
Mike Stump620d57a2009-10-12 20:50:45 +0000368 const char* vs90comntools = getenv("VS90COMNTOOLS");
369 const char* vs80comntools = getenv("VS80COMNTOOLS");
370 const char* vscomntools = NULL;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000371
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000372 // Try to find the version that we were compiled with
Douglas Gregor80f93d92010-05-18 05:47:04 +0000373 if(false) {}
374 #if (_MSC_VER >= 1600) // VC100
375 else if(vs100comntools) {
376 vscomntools = vs100comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000377 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000378 #elif (_MSC_VER == 1500) // VC80
379 else if(vs90comntools) {
380 vscomntools = vs90comntools;
381 }
382 #elif (_MSC_VER == 1400) // VC80
383 else if(vs80comntools) {
384 vscomntools = vs80comntools;
385 }
386 #endif
387 // Otherwise find any version we can
388 else if (vs100comntools)
389 vscomntools = vs100comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000390 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000391 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000392 else if (vs80comntools)
393 vscomntools = vs80comntools;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000394
Mike Stump620d57a2009-10-12 20:50:45 +0000395 if (vscomntools && *vscomntools) {
Dan Gohmancb421fa2010-04-19 16:39:44 +0000396 char *p = const_cast<char *>(strstr(vscomntools, "\\Common7\\Tools"));
Mike Stump620d57a2009-10-12 20:50:45 +0000397 if (p)
398 *p = '\0';
399 path = vscomntools;
400 return(true);
401 }
402 else
403 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000404 }
Mike Stump620d57a2009-10-12 20:50:45 +0000405 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000406}
Mike Stump43d81762009-10-08 23:29:47 +0000407
John Thompson75ee3bd2009-11-21 00:15:52 +0000408 // Get Windows SDK installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000409static bool getWindowsSDKDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000410 char windowsSDKInstallDir[256];
411 // Try the Windows registry.
412 bool hasSDKDir = getSystemRegistryString(
413 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
414 "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000415 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000416 if (hasSDKDir && windowsSDKInstallDir[0]) {
417 path = windowsSDKInstallDir;
418 return(true);
419 }
420 return(false);
421}
422
mike-m79bc57c2010-05-16 19:03:52 +0000423void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
424 const HeaderSearchOptions &HSOpts) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000425 llvm::Triple::OSType os = triple.getOS();
426
427 switch (os) {
428 case llvm::Triple::NetBSD:
429 break;
430 default:
431 // FIXME: temporary hack: hard-coded paths.
432 AddPath("/usr/local/include", System, true, false, false);
433 break;
434 }
mike-m79bc57c2010-05-16 19:03:52 +0000435
436 // Builtin includes use #include_next directives and should be positioned
437 // just prior C include dirs.
438 if (HSOpts.UseBuiltinIncludes) {
439 // Ignore the sys root, we *always* look for clang headers relative to
440 // supplied path.
441 llvm::sys::Path P(HSOpts.ResourceDir);
442 P.appendComponent("include");
443 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
444 }
445
446 // Add dirs specified via 'configure --with-c-include-dirs'.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000447 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
448 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000449 llvm::SmallVector<llvm::StringRef, 5> dirs;
450 CIncludeDirs.split(dirs, ":");
451 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
452 i != dirs.end();
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000453 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000454 AddPath(*i, System, false, false, false);
455 return;
456 }
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000457
Mike Stump43d81762009-10-08 23:29:47 +0000458 switch (os) {
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000459 case llvm::Triple::Win32: {
460 std::string VSDir;
461 std::string WindowsSDKDir;
462 if (getVisualStudioDir(VSDir)) {
463 AddPath(VSDir + "\\VC\\include", System, false, false, false);
464 if (getWindowsSDKDir(WindowsSDKDir))
465 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
466 else
467 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
468 System, false, false, false);
469 } else {
470 // Default install paths.
471 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
472 System, false, false, false);
473 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
474 System, false, false, false);
475 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000476 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000477 System, false, false, false);
478 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
479 System, false, false, false);
480 AddPath(
John Thompson9319f022009-11-23 17:49:27 +0000481 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000482 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000483 }
484 break;
Daniel Dunbard11ee7f2010-09-09 17:38:18 +0000485 }
Chris Lattner86ed3a32010-04-11 19:29:39 +0000486 case llvm::Triple::Haiku:
487 AddPath("/boot/common/include", System, true, false, false);
488 AddPath("/boot/develop/headers/os", System, true, false, false);
489 AddPath("/boot/develop/headers/os/app", System, true, false, false);
490 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
491 AddPath("/boot/develop/headers/os/device", System, true, false, false);
492 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000493 AddPath("/boot/develop/headers/os/game", System, true, false, false);
Chris Lattner86ed3a32010-04-11 19:29:39 +0000494 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
495 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
496 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
497 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
498 AddPath("/boot/develop/headers/os/media", System, true, false, false);
499 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
500 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
501 AddPath("/boot/develop/headers/os/net", System, true, false, false);
502 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
503 AddPath("/boot/develop/headers/os/support", System, true, false, false);
504 AddPath("/boot/develop/headers/os/translation",
505 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000506 AddPath("/boot/develop/headers/os/add-ons/graphics",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000507 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000508 AddPath("/boot/develop/headers/os/add-ons/input_server",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000509 System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000510 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
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/tracker",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000513 System, true, false, false);
514 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
515 System, true, false, false);
516 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
517 System, true, false, false);
518 AddPath("/boot/develop/headers/os/be_apps/Tracker",
519 System, true, false, false);
520 AddPath("/boot/develop/headers/cpp", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000521 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
Chris Lattner86ed3a32010-04-11 19:29:39 +0000522 System, true, false, false);
523 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
524 AddPath("/boot/develop/headers/bsd", System, true, false, false);
525 AddPath("/boot/develop/headers/glibc", System, true, false, false);
526 AddPath("/boot/develop/headers/posix", System, true, false, false);
527 AddPath("/boot/develop/headers", System, true, false, false);
Eli Friedmana7e68452010-08-22 01:00:03 +0000528 break;
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000529 case llvm::Triple::Cygwin:
530 AddPath("/usr/include/w32api", System, true, false, false);
531 break;
Mike Stump620d57a2009-10-12 20:50:45 +0000532 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000533 AddPath("c:/mingw/include", System, true, false, false);
534 break;
535 default:
Mike Stump43d81762009-10-08 23:29:47 +0000536 break;
537 }
John Thompsond3f88342009-10-13 18:51:32 +0000538
John Thompsond3f88342009-10-13 18:51:32 +0000539 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000540}
541
Chris Lattner0e3cc052010-05-05 05:28:39 +0000542void InitHeaderSearch::
543AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000544 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000545 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
546 if (CxxIncludeRoot != "") {
547 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
548 if (CxxIncludeArch == "")
549 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
Chris Lattner0e3cc052010-05-05 05:28:39 +0000550 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
551 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000552 else
553 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
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 return;
557 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000558 // FIXME: temporary hack: hard-coded paths.
559 switch (os) {
560 case llvm::Triple::Cygwin:
NAKAMURA Takumi32df0022010-10-11 02:27:37 +0000561 // Cygwin-1.7
562 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
563 // g++-4 / Cygwin-1.5
564 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
565 // FIXME: Do we support g++-3.4.4?
566 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000567 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000568 case llvm::Triple::MinGW32:
NAKAMURA Takumi05c699e2011-02-17 08:51:47 +0000569 // mingw-w64-20110207
570 AddPath("c:/MinGW/include/c++/4.5.3", System, true, false, false);
571 AddPath("c:/MinGW/include/c++/4.5.3/x86_64-w64-mingw32", System, true, false, false);
572 AddPath("c:/MinGW/include/c++/4.5.3/backward", System, true, false, false);
573 // mingw-w64-20101129
574 AddPath("c:/MinGW/include/c++/4.5.2", System, true, false, false);
575 AddPath("c:/MinGW/include/c++/4.5.2/x86_64-w64-mingw32", System, true, false, false);
576 AddPath("c:/MinGW/include/c++/4.5.2/backward", System, true, false, false);
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000577 // Try gcc 4.5.0
578 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000579 // Try gcc 4.4.0
580 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
581 // Try gcc 4.3.0
582 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
583 break;
584 case llvm::Triple::Darwin:
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000585 switch (triple.getArch()) {
586 default: break;
587
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000588 case llvm::Triple::ppc:
Douglas Gregor582c3012010-05-29 01:15:12 +0000589 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000590 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000591 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor616d4362010-05-29 01:21:11 +0000592 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000593 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +0000594 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor582c3012010-05-29 01:15:12 +0000595 triple);
596 break;
597
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000598 case llvm::Triple::x86:
599 case llvm::Triple::x86_64:
600 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
601 "i686-apple-darwin10", "", "x86_64", triple);
602 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
603 "i686-apple-darwin8", "", "", triple);
604 break;
605
606 case llvm::Triple::arm:
607 case llvm::Triple::thumb:
608 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
609 "arm-apple-darwin10", "v7", "", triple);
610 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
611 "arm-apple-darwin10", "v6", "", triple);
612 break;
613 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000614 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000615 case llvm::Triple::DragonFly:
616 AddPath("/usr/include/c++/4.1", System, true, false, false);
617 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000618 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000619 //===------------------------------------------------------------------===//
620 // Debian based distros.
621 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
622 //===------------------------------------------------------------------===//
Rafael Espindolac12bbe52011-02-15 21:44:06 +0000623 // Ubuntu 10.10 "Maverick Meerkat" -- gcc-4.4.5
Rafael Espindolaadafdba2011-02-15 21:16:43 +0000624 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
625 "i686-linux-gnu", "", "64", triple);
626 // The rest of 10.10 is the same as previous versions.
627
mike-mac78b7a2010-05-06 14:11:13 +0000628 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
629 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
630 // Debian 6.0 "squeeze" -- gcc-4.4.2
631 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
632 "x86_64-linux-gnu", "32", "", triple);
633 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
634 "i486-linux-gnu", "", "64", triple);
Nick Lewyckydb083552011-02-01 21:32:14 +0000635 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
636 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000637 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
638 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
639 // Debian 5.0 "lenny" -- gcc-4.3.2
640 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
641 "x86_64-linux-gnu", "32", "", triple);
642 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
643 "i486-linux-gnu", "", "64", triple);
Rafael Espindolae7d6c2c2010-06-04 14:28:10 +0000644 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
645 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000646 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
647 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
648 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
649 "x86_64-linux-gnu", "32", "", triple);
650 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
651 "i486-linux-gnu", "", "64", triple);
652 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
653 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
654 "x86_64-linux-gnu", "32", "", triple);
655 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
656 "i486-linux-gnu", "", "64", triple);
657
658 //===------------------------------------------------------------------===//
659 // Redhat based distros.
660 //===------------------------------------------------------------------===//
Rafael Espindola6638b3a2010-11-02 18:39:34 +0000661 // Fedora 14
662 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
663 "x86_64-redhat-linux", "32", "", triple);
664 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
665 "i686-redhat-linux", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000666 // Fedora 13
Chris Lattner4336e192010-05-29 01:01:38 +0000667 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
668 "x86_64-redhat-linux", "32", "", triple);
669 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
670 "i686-redhat-linux","", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000671 // Fedora 12
672 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
673 "x86_64-redhat-linux", "32", "", triple);
674 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
675 "i686-redhat-linux","", "", triple);
676 // Fedora 12 (pre-FEB-2010)
677 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
678 "x86_64-redhat-linux", "32", "", triple);
679 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
680 "i686-redhat-linux","", "", triple);
681 // Fedora 11
682 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
683 "x86_64-redhat-linux", "32", "", triple);
684 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
685 "i586-redhat-linux","", "", triple);
686 // Fedora 10
687 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
688 "x86_64-redhat-linux", "32", "", triple);
689 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
690 "i386-redhat-linux","", "", triple);
691 // Fedora 9
692 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
693 "x86_64-redhat-linux", "32", "", triple);
694 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
695 "i386-redhat-linux", "", "", triple);
696 // Fedora 8
697 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
698 "x86_64-redhat-linux", "", "", triple);
699 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
700 "i386-redhat-linux", "", "", triple);
701
702 //===------------------------------------------------------------------===//
703
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000704 // Exherbo (2010-01-25)
705 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000706 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000707 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000708 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000709
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000710 // openSUSE 11.1 32 bit
711 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000712 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000713 // openSUSE 11.1 64 bit
714 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000715 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000716 // openSUSE 11.2
717 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000718 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000719 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000720 "x86_64-suse-linux", "", "", triple);
Rafael Espindola9d2c0602010-11-25 18:51:59 +0000721
722 // openSUSE 11.4
723 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
724 "i586-suse-linux", "", "", triple);
725 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
726 "x86_64-suse-linux", "", "", triple);
727
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000728 // Arch Linux 2008-06-24
729 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000730 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000731 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000732 "x86_64-unknown-linux-gnu", "", "", triple);
Nuno Lopes0d155a52010-09-11 17:51:45 +0000733 // Gentoo x86 2010.0 stable
734 AddGnuCPlusPlusIncludePaths(
735 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
736 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000737 // Gentoo x86 2009.1 stable
738 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000739 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000740 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000741 // Gentoo x86 2009.0 stable
742 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000743 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000744 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000745 // Gentoo x86 2008.0 stable
746 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000747 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000748 "i686-pc-linux-gnu", "", "", triple);
Nick Lewycky66935342010-07-24 21:33:13 +0000749
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000750 // Gentoo amd64 gcc 4.4.5
751 AddGnuCPlusPlusIncludePaths(
752 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4",
753 "x86_64-pc-linux-gnu", "32", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000754 // Gentoo amd64 gcc 4.4.4
755 AddGnuCPlusPlusIncludePaths(
756 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
757 "x86_64-pc-linux-gnu", "32", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000758 // Gentoo amd64 gcc 4.4.3
759 AddGnuCPlusPlusIncludePaths(
760 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
761 "x86_64-pc-linux-gnu", "32", "", triple);
762 // Gentoo amd64 gcc 4.3.2
763 AddGnuCPlusPlusIncludePaths(
764 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
765 "x86_64-pc-linux-gnu", "", "", triple);
766 // Gentoo amd64 stable
767 AddGnuCPlusPlusIncludePaths(
768 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
769 "i686-pc-linux-gnu", "", "", triple);
Nico Weber8ad8a1c2010-11-16 12:42:55 +0000770
Nick Lewycky66935342010-07-24 21:33:13 +0000771 // Gentoo amd64 llvm-gcc trunk
772 AddGnuCPlusPlusIncludePaths(
773 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
774 "x86_64-pc-linux-gnu", "", "", triple);
Chandler Carruthfbfdb202010-11-28 07:20:14 +0000775
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000776 break;
777 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000778 // FreeBSD 8.0
779 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000780 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000781 break;
Anton Korobeynikovab079412010-08-31 22:39:50 +0000782 case llvm::Triple::NetBSD:
783 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
784 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000785 case llvm::Triple::OpenBSD: {
786 std::string t = triple.getTriple();
787 if (t.substr(0, 6) == "x86_64")
788 t.replace(0, 6, "amd64");
789 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
790 t, "", "", triple);
791 break;
792 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000793 case llvm::Triple::Minix:
794 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
795 "", "", "", triple);
796 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000797 case llvm::Triple::Solaris:
798 // Solaris - Fall though..
799 case llvm::Triple::AuroraUX:
800 // AuroraUX
801 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000802 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000803 break;
804 default:
805 break;
806 }
807}
808
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000809void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000810 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000811 const HeaderSearchOptions &HSOpts) {
Daniel Dunbar80c26f42010-09-09 17:38:22 +0000812 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes) {
813 if (!HSOpts.CXXSystemIncludes.empty()) {
814 for (unsigned i = 0, e = HSOpts.CXXSystemIncludes.size(); i != e; ++i)
815 AddPath(HSOpts.CXXSystemIncludes[i], System, true, false, false);
816 } else
817 AddDefaultCPlusPlusIncludePaths(triple);
818 }
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000819
mike-m79bc57c2010-05-16 19:03:52 +0000820 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000821
822 // Add the default framework include paths on Darwin.
823 if (triple.getOS() == llvm::Triple::Darwin) {
824 AddPath("/System/Library/Frameworks", System, true, false, true);
825 AddPath("/Library/Frameworks", System, true, false, true);
826 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000827}
828
Nico Weber0fca0222008-08-22 09:25:22 +0000829/// RemoveDuplicates - If there are duplicate directory entries in the specified
830/// search list, remove the later (dead) ones.
831static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
832 bool Verbose) {
833 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
834 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
835 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
836 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000837 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Chris Lattner43eee072009-02-08 01:00:10 +0000839 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Chris Lattner43eee072009-02-08 01:00:10 +0000841 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000842 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000843 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000844 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000845 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000846 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000847 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000848 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000849 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000850 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000851 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000852 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000853 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000854 }
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Chris Lattner30f05b52009-02-08 00:55:22 +0000856 // If we have a normal #include dir/framework/headermap that is shadowed
857 // later in the chain by a system include location, we actually want to
858 // ignore the user's request and drop the user dir... keeping the system
859 // dir. This is weird, but required to emulate GCC's search path correctly.
860 //
861 // Since dupes of system dirs are rare, just rescan to find the original
862 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000863 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000864 // Find the dir that this is the same of.
865 unsigned FirstDir;
866 for (FirstDir = 0; ; ++FirstDir) {
867 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Chris Lattner43eee072009-02-08 01:00:10 +0000869 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
870
Chris Lattner30f05b52009-02-08 00:55:22 +0000871 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000872 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000873 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Chris Lattner30f05b52009-02-08 00:55:22 +0000875 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000876 if (CurEntry.isNormalDir())
877 isSame = SearchEntry.getDir() == CurEntry.getDir();
878 else if (CurEntry.isFramework())
879 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000880 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000881 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
882 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Chris Lattner30f05b52009-02-08 00:55:22 +0000885 if (isSame)
886 break;
887 }
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Chris Lattner30f05b52009-02-08 00:55:22 +0000889 // If the first dir in the search path is a non-system dir, zap it
890 // instead of the system one.
891 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
892 DirToRemove = FirstDir;
893 }
894
895 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000896 llvm::errs() << "ignoring duplicate directory \""
897 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +0000898 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000899 llvm::errs() << " as it is a non-system directory that duplicates "
900 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000901 }
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Chris Lattner7a739402008-09-26 17:46:45 +0000903 // This is reached if the current entry is a duplicate. Remove the
904 // DirToRemove (usually the current dir).
905 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000906 --i;
907 }
908}
909
910
911void InitHeaderSearch::Realize() {
912 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
913 std::vector<DirectoryLookup> SearchList;
914 SearchList = IncludeGroup[Angled];
915 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
916 IncludeGroup[System].end());
917 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
918 IncludeGroup[After].end());
919 RemoveDuplicates(SearchList, Verbose);
920 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Nico Weber0fca0222008-08-22 09:25:22 +0000922 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000923 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000924 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Nico Weber0fca0222008-08-22 09:25:22 +0000926
927 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
928 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
929 DontSearchCurDir);
930
931 // If verbose, print the list of directories that will be searched.
932 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000933 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000934 unsigned QuotedIdx = IncludeGroup[Quoted].size();
935 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
936 if (i == QuotedIdx)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000937 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000938 const char *Name = SearchList[i].getName();
939 const char *Suffix;
940 if (SearchList[i].isNormalDir())
941 Suffix = "";
942 else if (SearchList[i].isFramework())
943 Suffix = " (framework directory)";
944 else {
945 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
946 Suffix = " (headermap)";
947 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000948 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000949 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000950 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000951 }
952}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000953
Daniel Dunbar5814e652009-11-11 21:44:21 +0000954void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
955 const HeaderSearchOptions &HSOpts,
956 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000957 const llvm::Triple &Triple) {
958 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
959
960 // Add the user defined entries.
961 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
962 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +0000963 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
Chris Lattner23637be2010-08-24 22:27:37 +0000964 !E.IsSysRootRelative);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000965 }
966
967 // Add entries from CPATH and friends.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000968 Init.AddDelimitedPaths(HSOpts.EnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000969 if (Lang.CPlusPlus && Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000970 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000971 else if (Lang.CPlusPlus)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000972 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000973 else if (Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000974 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000975 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000976 Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000977
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000978 if (HSOpts.UseStandardIncludes)
mike-m79bc57c2010-05-16 19:03:52 +0000979 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000980
981 Init.Realize();
982}