blob: f03502a4384756ea34d9ceec12fd0818923428b6 [file] [log] [blame]
Nick Lewyckydf22c2c2010-07-25 03:12:58 +00001//===--- InitHeaderSearch.cpp - Initialize header search paths ------------===//
Nico Weber0fca0222008-08-22 09:25:22 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the InitHeaderSearch class.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000014#include "clang/Frontend/Utils.h"
Nico Weber0fca0222008-08-22 09:25:22 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/LangOptions.h"
Daniel Dunbar63c8b772009-11-07 04:20:50 +000017#include "clang/Frontend/HeaderSearchOptions.h"
18#include "clang/Lex/HeaderSearch.h"
Nico Weber0fca0222008-08-22 09:25:22 +000019#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/SmallPtrSet.h"
Rafael Espindolaaadd7a42009-11-13 05:13:58 +000021#include "llvm/ADT/SmallVector.h"
Rafael Espindolaf0a2f512009-11-12 05:48:41 +000022#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000023#include "llvm/ADT/Triple.h"
Benjamin Kramere89ba592009-12-08 12:38:20 +000024#include "llvm/ADT/Twine.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000025#include "llvm/Support/raw_ostream.h"
Nico Weber0fca0222008-08-22 09:25:22 +000026#include "llvm/System/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000027#include "llvm/Config/config.h"
Mike Stump620d57a2009-10-12 20:50:45 +000028#ifdef _MSC_VER
29 #define WIN32_LEAN_AND_MEAN 1
30 #include <windows.h>
31#endif
Nico Weber0fca0222008-08-22 09:25:22 +000032using namespace clang;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000033using namespace clang::frontend;
34
35namespace {
36
37/// InitHeaderSearch - This class makes it easier to set the search paths of
38/// a HeaderSearch object. InitHeaderSearch stores several search path lists
39/// internally, which can be sent to a HeaderSearch object in one swoop.
40class InitHeaderSearch {
41 std::vector<DirectoryLookup> IncludeGroup[4];
42 HeaderSearch& Headers;
43 bool Verbose;
44 std::string isysroot;
45
46public:
47
48 InitHeaderSearch(HeaderSearch &HS,
49 bool verbose = false, const std::string &iSysroot = "")
50 : Headers(HS), Verbose(verbose), isysroot(iSysroot) {}
51
52 /// AddPath - Add the specified path to the specified group list.
Benjamin Kramere89ba592009-12-08 12:38:20 +000053 void AddPath(const llvm::Twine &Path, IncludeDirGroup Group,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000054 bool isCXXAware, bool isUserSupplied,
55 bool isFramework, bool IgnoreSysRoot = false);
56
mike-ma6087372010-05-05 17:00:31 +000057 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000058 /// libstdc++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000059 void AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
60 llvm::StringRef ArchDir,
61 llvm::StringRef Dir32,
62 llvm::StringRef Dir64,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000063 const llvm::Triple &triple);
64
65 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW
66 /// libstdc++.
Benjamin Kramere89ba592009-12-08 12:38:20 +000067 void AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
68 llvm::StringRef Arch,
69 llvm::StringRef Version);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000070
71 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
72 /// separator. The processing follows that of the CPATH variable for gcc.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +000073 void AddDelimitedPaths(llvm::StringRef String);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000074
75 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-m79bc57c2010-05-16 19:03:52 +000076 void AddDefaultCIncludePaths(const llvm::Triple &triple,
77 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000078
79 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
80 // compiling c++.
81 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
82
83 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
84 /// that e.g. stdio.h is found.
85 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +000086 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +000087 const HeaderSearchOptions &HSOpts);
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000088
89 /// Realize - Merges all search path lists into one list and send it to
90 /// HeaderSearch.
91 void Realize();
92};
93
94}
Nico Weber0fca0222008-08-22 09:25:22 +000095
Benjamin Kramere89ba592009-12-08 12:38:20 +000096void InitHeaderSearch::AddPath(const llvm::Twine &Path,
Benjamin Kramer458fb102009-09-05 09:49:39 +000097 IncludeDirGroup Group, bool isCXXAware,
98 bool isUserSupplied, bool isFramework,
99 bool IgnoreSysRoot) {
Benjamin Kramere89ba592009-12-08 12:38:20 +0000100 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
Nico Weber0fca0222008-08-22 09:25:22 +0000101 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Nico Weber0fca0222008-08-22 09:25:22 +0000103 // Compute the actual path, taking into consideration -isysroot.
Benjamin Kramere89ba592009-12-08 12:38:20 +0000104 llvm::SmallString<256> MappedPathStr;
105 llvm::raw_svector_ostream MappedPath(MappedPathStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Nico Weber0fca0222008-08-22 09:25:22 +0000107 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +0000108 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +0000109 // FIXME: Portability. This should be a sys::Path interface, this doesn't
110 // handle things like C:\ right, nor win32 \\network\device\blah.
111 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
Benjamin Kramere89ba592009-12-08 12:38:20 +0000112 MappedPath << isysroot;
Nico Weber0fca0222008-08-22 09:25:22 +0000113 }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Benjamin Kramere89ba592009-12-08 12:38:20 +0000115 Path.print(MappedPath);
Nico Weber0fca0222008-08-22 09:25:22 +0000116
117 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000118 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000119 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000120 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000121 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000122 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000123 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000124 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000125
126
Nico Weber0fca0222008-08-22 09:25:22 +0000127 // If the directory exists, add it.
Benjamin Kramer458fb102009-09-05 09:49:39 +0000128 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000129 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
130 isFramework));
131 return;
132 }
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Nico Weber0fca0222008-08-22 09:25:22 +0000134 // Check to see if this is an apple-style headermap (which are not allowed to
135 // be frameworks).
136 if (!isFramework) {
Benjamin Kramer458fb102009-09-05 09:49:39 +0000137 if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000138 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
139 // It is a headermap, add it to the search path.
140 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
141 return;
142 }
143 }
144 }
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Nico Weber0fca0222008-08-22 09:25:22 +0000146 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +0000147 llvm::errs() << "ignoring nonexistent directory \""
148 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000149}
150
151
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000152void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) {
153 if (at.empty()) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000154 return;
155
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000156 llvm::StringRef::size_type delim;
157 while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) {
158 if (delim == 0)
Nico Weber0fca0222008-08-22 09:25:22 +0000159 AddPath(".", Angled, false, true, false);
160 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000161 AddPath(at.substr(0, delim), Angled, false, true, false);
162 at = at.substr(delim + 1);
Nico Weber0fca0222008-08-22 09:25:22 +0000163 }
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000164
165 if (at.empty())
Nico Weber0fca0222008-08-22 09:25:22 +0000166 AddPath(".", Angled, false, true, false);
167 else
168 AddPath(at, Angled, false, true, false);
169}
170
Benjamin Kramere89ba592009-12-08 12:38:20 +0000171void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
172 llvm::StringRef ArchDir,
173 llvm::StringRef Dir32,
174 llvm::StringRef Dir64,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000175 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000176 // Add the base dir
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000177 AddPath(Base, System, true, false, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000178
179 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000180 llvm::Triple::ArchType arch = triple.getArch();
181 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000182 if (is64bit)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000183 AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000184 else
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000185 AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000186
187 // Add the backward dir
188 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000189}
Nico Weber0fca0222008-08-22 09:25:22 +0000190
Benjamin Kramere89ba592009-12-08 12:38:20 +0000191void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
192 llvm::StringRef Arch,
193 llvm::StringRef Version) {
Benjamin Kramerab8ae192010-01-20 16:18:11 +0000194 AddPath(Base + "/" + Arch + "/" + Version + "/include",
195 System, true, false, false);
196 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
197 System, true, false, false);
198 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
199 System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000200}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000201
Mike Stump620d57a2009-10-12 20:50:45 +0000202 // FIXME: This probably should goto to some platform utils place.
203#ifdef _MSC_VER
John Thompson75ee3bd2009-11-21 00:15:52 +0000204
Mike Stump620d57a2009-10-12 20:50:45 +0000205 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000206 // This also supports a means to look for high-versioned keys by use
207 // of a $VERSION placeholder in the key path.
208 // $VERSION in the key path is a placeholder for the version number,
209 // causing the highest value path to be searched for and used.
210 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
211 // There can be additional characters in the component. Only the numberic
212 // characters are compared.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000213static bool getSystemRegistryString(const char *keyPath, const char *valueName,
214 char *value, size_t maxLength) {
Mike Stump43d81762009-10-08 23:29:47 +0000215 HKEY hRootKey = NULL;
216 HKEY hKey = NULL;
217 const char* subKey = NULL;
218 DWORD valueType;
219 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000220 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000221 bool returnValue = false;
222 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
223 hRootKey = HKEY_CLASSES_ROOT;
224 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000225 }
Mike Stump43d81762009-10-08 23:29:47 +0000226 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
227 hRootKey = HKEY_USERS;
228 subKey = keyPath + 11;
229 }
230 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
231 hRootKey = HKEY_LOCAL_MACHINE;
232 subKey = keyPath + 19;
233 }
234 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
235 hRootKey = HKEY_CURRENT_USER;
236 subKey = keyPath + 18;
237 }
238 else
239 return(false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000240 const char *placeHolder = strstr(subKey, "$VERSION");
241 char bestName[256];
242 bestName[0] = '\0';
243 // If we have a $VERSION placeholder, do the highest-version search.
244 if (placeHolder) {
245 const char *keyEnd = placeHolder - 1;
246 const char *nextKey = placeHolder;
247 // Find end of previous key.
248 while ((keyEnd > subKey) && (*keyEnd != '\\'))
249 keyEnd--;
250 // Find end of key containing $VERSION.
251 while (*nextKey && (*nextKey != '\\'))
252 nextKey++;
253 size_t partialKeyLength = keyEnd - subKey;
254 char partialKey[256];
255 if (partialKeyLength > sizeof(partialKey))
256 partialKeyLength = sizeof(partialKey);
257 strncpy(partialKey, subKey, partialKeyLength);
258 partialKey[partialKeyLength] = '\0';
259 HKEY hTopKey = NULL;
260 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
261 if (lResult == ERROR_SUCCESS) {
262 char keyName[256];
263 int bestIndex = -1;
264 double bestValue = 0.0;
265 DWORD index, size = sizeof(keyName) - 1;
Nuno Lopes33cc2432009-12-07 17:18:48 +0000266 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
267 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
268 const char *sp = keyName;
269 while (*sp && !isdigit(*sp))
270 sp++;
271 if (!*sp)
272 continue;
273 const char *ep = sp + 1;
274 while (*ep && (isdigit(*ep) || (*ep == '.')))
275 ep++;
276 char numBuf[32];
277 strncpy(numBuf, sp, sizeof(numBuf) - 1);
278 numBuf[sizeof(numBuf) - 1] = '\0';
279 double value = strtod(numBuf, NULL);
280 if (value > bestValue) {
281 bestIndex = (int)index;
282 bestValue = value;
283 strcpy(bestName, keyName);
284 }
John Thompson75ee3bd2009-11-21 00:15:52 +0000285 size = sizeof(keyName) - 1;
286 }
287 // If we found the highest versioned key, open the key and get the value.
288 if (bestIndex != -1) {
289 // Append rest of key.
290 strncat(bestName, nextKey, sizeof(bestName) - 1);
291 bestName[sizeof(bestName) - 1] = '\0';
292 // Open the chosen key path remainder.
293 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
294 if (lResult == ERROR_SUCCESS) {
295 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
296 (LPBYTE)value, &valueSize);
297 if (lResult == ERROR_SUCCESS)
298 returnValue = true;
299 RegCloseKey(hKey);
300 }
301 }
302 RegCloseKey(hTopKey);
303 }
304 }
305 else {
306 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
307 if (lResult == ERROR_SUCCESS) {
308 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
309 (LPBYTE)value, &valueSize);
310 if (lResult == ERROR_SUCCESS)
311 returnValue = true;
312 RegCloseKey(hKey);
313 }
Mike Stump43d81762009-10-08 23:29:47 +0000314 }
315 return(returnValue);
316}
Mike Stump620d57a2009-10-12 20:50:45 +0000317#else // _MSC_VER
318 // Read registry string.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000319static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
Mike Stump620d57a2009-10-12 20:50:45 +0000320 return(false);
321}
322#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000323
Mike Stump43d81762009-10-08 23:29:47 +0000324 // Get Visual Studio installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000325static bool getVisualStudioDir(std::string &path) {
Michael J. Spencerff58e362010-08-21 21:55:07 +0000326 // First check the environment variables that vsvars32.bat sets.
327 const char* vcinstalldir = getenv("VCINSTALLDIR");
328 if(vcinstalldir) {
329 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
330 if (p)
331 *p = '\0';
332 path = vcinstalldir;
333 return(true);
334 }
335
John Thompson75ee3bd2009-11-21 00:15:52 +0000336 char vsIDEInstallDir[256];
Douglas Gregor80f93d92010-05-18 05:47:04 +0000337 char vsExpressIDEInstallDir[256];
Michael J. Spencerff58e362010-08-21 21:55:07 +0000338 // Then try the windows registry.
John Thompson75ee3bd2009-11-21 00:15:52 +0000339 bool hasVCDir = getSystemRegistryString(
340 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
341 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Douglas Gregor80f93d92010-05-18 05:47:04 +0000342 bool hasVCExpressDir = getSystemRegistryString(
343 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
344 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000345 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000346 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000347 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
348 if (p)
349 *p = '\0';
350 path = vsIDEInstallDir;
351 return(true);
352 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000353 else if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
354 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
355 if (p)
356 *p = '\0';
357 path = vsExpressIDEInstallDir;
358 return(true);
359 }
Mike Stump620d57a2009-10-12 20:50:45 +0000360 else {
361 // Try the environment.
Douglas Gregor80f93d92010-05-18 05:47:04 +0000362 const char* vs100comntools = getenv("VS100COMNTOOLS");
Mike Stump620d57a2009-10-12 20:50:45 +0000363 const char* vs90comntools = getenv("VS90COMNTOOLS");
364 const char* vs80comntools = getenv("VS80COMNTOOLS");
365 const char* vscomntools = NULL;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000366
367 // Try to find the version that we were compiled with
368 if(false) {}
369 #if (_MSC_VER >= 1600) // VC100
370 else if(vs100comntools) {
371 vscomntools = vs100comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000372 }
Douglas Gregor80f93d92010-05-18 05:47:04 +0000373 #elif (_MSC_VER == 1500) // VC80
374 else if(vs90comntools) {
375 vscomntools = vs90comntools;
376 }
377 #elif (_MSC_VER == 1400) // VC80
378 else if(vs80comntools) {
379 vscomntools = vs80comntools;
380 }
381 #endif
382 // Otherwise find any version we can
383 else if (vs100comntools)
384 vscomntools = vs100comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000385 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000386 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000387 else if (vs80comntools)
388 vscomntools = vs80comntools;
Douglas Gregor80f93d92010-05-18 05:47:04 +0000389
Mike Stump620d57a2009-10-12 20:50:45 +0000390 if (vscomntools && *vscomntools) {
Dan Gohmancb421fa2010-04-19 16:39:44 +0000391 char *p = const_cast<char *>(strstr(vscomntools, "\\Common7\\Tools"));
Mike Stump620d57a2009-10-12 20:50:45 +0000392 if (p)
393 *p = '\0';
394 path = vscomntools;
395 return(true);
396 }
397 else
398 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000399 }
Mike Stump620d57a2009-10-12 20:50:45 +0000400 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000401}
Mike Stump43d81762009-10-08 23:29:47 +0000402
John Thompson75ee3bd2009-11-21 00:15:52 +0000403 // Get Windows SDK installation directory.
Benjamin Kramer6cd52162010-01-20 16:21:40 +0000404static bool getWindowsSDKDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000405 char windowsSDKInstallDir[256];
406 // Try the Windows registry.
407 bool hasSDKDir = getSystemRegistryString(
408 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
409 "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1);
410 // If we have both vc80 and vc90, pick version we were compiled with.
411 if (hasSDKDir && windowsSDKInstallDir[0]) {
412 path = windowsSDKInstallDir;
413 return(true);
414 }
415 return(false);
416}
417
mike-m79bc57c2010-05-16 19:03:52 +0000418void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
419 const HeaderSearchOptions &HSOpts) {
Mike Stump43d81762009-10-08 23:29:47 +0000420 // FIXME: temporary hack: hard-coded paths.
mike-m79bc57c2010-05-16 19:03:52 +0000421 AddPath("/usr/local/include", System, true, false, false);
422
423 // Builtin includes use #include_next directives and should be positioned
424 // just prior C include dirs.
425 if (HSOpts.UseBuiltinIncludes) {
426 // Ignore the sys root, we *always* look for clang headers relative to
427 // supplied path.
428 llvm::sys::Path P(HSOpts.ResourceDir);
429 P.appendComponent("include");
430 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
431 }
432
433 // Add dirs specified via 'configure --with-c-include-dirs'.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000434 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
435 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000436 llvm::SmallVector<llvm::StringRef, 5> dirs;
437 CIncludeDirs.split(dirs, ":");
438 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
439 i != dirs.end();
440 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000441 AddPath(*i, System, false, false, false);
442 return;
443 }
Mike Stump43d81762009-10-08 23:29:47 +0000444 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000445 switch (os) {
446 case llvm::Triple::Win32:
447 {
Mike Stump620d57a2009-10-12 20:50:45 +0000448 std::string VSDir;
John Thompson75ee3bd2009-11-21 00:15:52 +0000449 std::string WindowsSDKDir;
Mike Stump620d57a2009-10-12 20:50:45 +0000450 if (getVisualStudioDir(VSDir)) {
451 AddPath(VSDir + "\\VC\\include", System, false, false, false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000452 if (getWindowsSDKDir(WindowsSDKDir))
Michael J. Spencerff58e362010-08-21 21:55:07 +0000453 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000454 else
455 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
456 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000457 }
John Thompson9319f022009-11-23 17:49:27 +0000458 else {
459 // Default install paths.
Douglas Gregor80f93d92010-05-18 05:47:04 +0000460 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
461 System, false, false, false);
John Thompson9319f022009-11-23 17:49:27 +0000462 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
463 System, false, false, false);
464 AddPath(
465 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
466 System, false, false, false);
467 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
468 System, false, false, false);
469 AddPath(
470 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
471 System, false, false, false);
472 // For some clang developers.
473 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
474 System, false, false, false);
475 AddPath(
476 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
477 System, false, false, false);
478 }
Mike Stump43d81762009-10-08 23:29:47 +0000479 }
480 break;
Chris Lattner86ed3a32010-04-11 19:29:39 +0000481 case llvm::Triple::Haiku:
482 AddPath("/boot/common/include", System, true, false, false);
483 AddPath("/boot/develop/headers/os", System, true, false, false);
484 AddPath("/boot/develop/headers/os/app", System, true, false, false);
485 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
486 AddPath("/boot/develop/headers/os/device", System, true, false, false);
487 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
488 AddPath("/boot/develop/headers/os/game", System, true, false, false);
489 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
490 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
491 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
492 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
493 AddPath("/boot/develop/headers/os/media", System, true, false, false);
494 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
495 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
496 AddPath("/boot/develop/headers/os/net", System, true, false, false);
497 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
498 AddPath("/boot/develop/headers/os/support", System, true, false, false);
499 AddPath("/boot/develop/headers/os/translation",
500 System, true, false, false);
501 AddPath("/boot/develop/headers/os/add-ons/graphics",
502 System, true, false, false);
503 AddPath("/boot/develop/headers/os/add-ons/input_server",
504 System, true, false, false);
505 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
506 System, true, false, false);
507 AddPath("/boot/develop/headers/os/add-ons/tracker",
508 System, true, false, false);
509 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
510 System, true, false, false);
511 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
512 System, true, false, false);
513 AddPath("/boot/develop/headers/os/be_apps/Tracker",
514 System, true, false, false);
515 AddPath("/boot/develop/headers/cpp", System, true, false, false);
516 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
517 System, true, false, false);
518 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
519 AddPath("/boot/develop/headers/bsd", System, true, false, false);
520 AddPath("/boot/develop/headers/glibc", System, true, false, false);
521 AddPath("/boot/develop/headers/posix", System, true, false, false);
522 AddPath("/boot/develop/headers", System, true, false, false);
523 break;
Mike Stump43d81762009-10-08 23:29:47 +0000524 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000525 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000526 AddPath("c:/mingw/include", System, true, false, false);
527 break;
528 default:
Mike Stump43d81762009-10-08 23:29:47 +0000529 break;
530 }
John Thompsond3f88342009-10-13 18:51:32 +0000531
John Thompsond3f88342009-10-13 18:51:32 +0000532 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000533}
534
Chris Lattner0e3cc052010-05-05 05:28:39 +0000535void InitHeaderSearch::
536AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000537 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000538 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
539 if (CxxIncludeRoot != "") {
540 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
541 if (CxxIncludeArch == "")
542 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
Chris Lattner0e3cc052010-05-05 05:28:39 +0000543 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
544 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000545 else
546 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
Chris Lattner0e3cc052010-05-05 05:28:39 +0000547 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
548 triple);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000549 return;
550 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000551 // FIXME: temporary hack: hard-coded paths.
552 switch (os) {
553 case llvm::Triple::Cygwin:
554 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
555 System, true, false, false);
556 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
557 System, true, false, false);
Douglas Gregorea9e56d2010-06-16 16:24:51 +0000558 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/i686-pc-cygwin",
559 System, true, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000560 break;
561 case llvm::Triple::MinGW64:
562 // Try gcc 4.4.0
563 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
564 // Try gcc 4.3.0
565 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
566 // Fall through.
567 case llvm::Triple::MinGW32:
568 // Try gcc 4.4.0
569 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
570 // Try gcc 4.3.0
571 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
572 break;
573 case llvm::Triple::Darwin:
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000574 switch (triple.getArch()) {
575 default: break;
576
Douglas Gregor582c3012010-05-29 01:15:12 +0000577 case llvm::Triple::ppc:
578 case llvm::Triple::ppc64:
Douglas Gregor616d4362010-05-29 01:21:11 +0000579 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
580 "powerpc-apple-darwin10", "", "ppc64",
581 triple);
Douglas Gregor582c3012010-05-29 01:15:12 +0000582 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
583 "powerpc-apple-darwin10", "", "ppc64",
584 triple);
585 break;
586
Daniel Dunbarf2070b32010-05-28 01:54:31 +0000587 case llvm::Triple::x86:
588 case llvm::Triple::x86_64:
589 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
590 "i686-apple-darwin10", "", "x86_64", triple);
591 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
592 "i686-apple-darwin8", "", "", triple);
593 break;
594
595 case llvm::Triple::arm:
596 case llvm::Triple::thumb:
597 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
598 "arm-apple-darwin10", "v7", "", triple);
599 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
600 "arm-apple-darwin10", "v6", "", triple);
601 break;
602 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000603 break;
Chris Lattner7a7ca282010-01-09 05:41:14 +0000604 case llvm::Triple::DragonFly:
605 AddPath("/usr/include/c++/4.1", System, true, false, false);
606 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000607 case llvm::Triple::Linux:
mike-mac78b7a2010-05-06 14:11:13 +0000608 //===------------------------------------------------------------------===//
609 // Debian based distros.
610 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
611 //===------------------------------------------------------------------===//
612 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
613 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
614 // Debian 6.0 "squeeze" -- gcc-4.4.2
615 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
616 "x86_64-linux-gnu", "32", "", triple);
617 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
618 "i486-linux-gnu", "", "64", triple);
619 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
620 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
621 // Debian 5.0 "lenny" -- gcc-4.3.2
622 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
623 "x86_64-linux-gnu", "32", "", triple);
624 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
625 "i486-linux-gnu", "", "64", triple);
Rafael Espindolae7d6c2c2010-06-04 14:28:10 +0000626 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
627 "arm-linux-gnueabi", "", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000628 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
629 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
630 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
631 "x86_64-linux-gnu", "32", "", triple);
632 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
633 "i486-linux-gnu", "", "64", triple);
634 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
635 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
636 "x86_64-linux-gnu", "32", "", triple);
637 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
638 "i486-linux-gnu", "", "64", triple);
639
640 //===------------------------------------------------------------------===//
641 // Redhat based distros.
642 //===------------------------------------------------------------------===//
643 // Fedora 13
Chris Lattner4336e192010-05-29 01:01:38 +0000644 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
645 "x86_64-redhat-linux", "32", "", triple);
646 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
647 "i686-redhat-linux","", "", triple);
mike-mac78b7a2010-05-06 14:11:13 +0000648 // Fedora 12
649 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
650 "x86_64-redhat-linux", "32", "", triple);
651 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
652 "i686-redhat-linux","", "", triple);
653 // Fedora 12 (pre-FEB-2010)
654 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
655 "x86_64-redhat-linux", "32", "", triple);
656 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
657 "i686-redhat-linux","", "", triple);
658 // Fedora 11
659 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
660 "x86_64-redhat-linux", "32", "", triple);
661 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
662 "i586-redhat-linux","", "", triple);
663 // Fedora 10
664 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
665 "x86_64-redhat-linux", "32", "", triple);
666 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
667 "i386-redhat-linux","", "", triple);
668 // Fedora 9
669 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
670 "x86_64-redhat-linux", "32", "", triple);
671 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
672 "i386-redhat-linux", "", "", triple);
673 // Fedora 8
674 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
675 "x86_64-redhat-linux", "", "", triple);
676 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
677 "i386-redhat-linux", "", "", triple);
678
679 //===------------------------------------------------------------------===//
680
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000681 // Exherbo (2010-01-25)
682 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000683 "x86_64-pc-linux-gnu", "32", "", triple);
Benjamin Kramer0db3d722010-01-25 12:20:15 +0000684 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
Torok Edwinfc4b8992009-12-18 17:29:14 +0000685 "i686-pc-linux-gnu", "", "", triple);
Chris Lattnerea00f842010-04-23 15:55:20 +0000686
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000687 // openSUSE 11.1 32 bit
688 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000689 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000690 // openSUSE 11.1 64 bit
691 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000692 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000693 // openSUSE 11.2
694 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000695 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000696 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000697 "x86_64-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000698 // Arch Linux 2008-06-24
699 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000700 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000701 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000702 "x86_64-unknown-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000703 // Gentoo x86 2009.1 stable
704 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000705 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000706 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000707 // Gentoo x86 2009.0 stable
708 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000709 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000710 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000711 // Gentoo x86 2008.0 stable
712 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000713 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000714 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000715 // Gentoo amd64 stable
716 AddGnuCPlusPlusIncludePaths(
717 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000718 "i686-pc-linux-gnu", "", "", triple);
Eric Christopher70d9d412010-03-03 21:41:50 +0000719
720 // Gentoo amd64 gcc 4.3.2
721 AddGnuCPlusPlusIncludePaths(
722 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
723 "x86_64-pc-linux-gnu", "", "", triple);
724
725 // Gentoo amd64 gcc 4.4.3
726 AddGnuCPlusPlusIncludePaths(
727 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
728 "x86_64-pc-linux-gnu", "32", "", triple);
Nick Lewycky66935342010-07-24 21:33:13 +0000729
730 // Gentoo amd64 llvm-gcc trunk
731 AddGnuCPlusPlusIncludePaths(
732 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
733 "x86_64-pc-linux-gnu", "", "", triple);
Eric Christopher70d9d412010-03-03 21:41:50 +0000734
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000735 break;
736 case llvm::Triple::FreeBSD:
mike-mac78b7a2010-05-06 14:11:13 +0000737 // FreeBSD 8.0
738 // FreeBSD 7.3
Nuno Lopesafe859a2010-01-17 00:00:11 +0000739 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000740 break;
Daniel Dunbar95c04572010-08-01 23:13:54 +0000741 case llvm::Triple::OpenBSD: {
742 std::string t = triple.getTriple();
743 if (t.substr(0, 6) == "x86_64")
744 t.replace(0, 6, "amd64");
745 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
746 t, "", "", triple);
747 break;
748 }
Chris Lattner38e317d2010-07-07 16:01:42 +0000749 case llvm::Triple::Minix:
750 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
751 "", "", "", triple);
752 break;
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000753 case llvm::Triple::Solaris:
754 // Solaris - Fall though..
755 case llvm::Triple::AuroraUX:
756 // AuroraUX
757 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000758 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000759 break;
760 default:
761 break;
762 }
763}
764
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000765void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000766 const llvm::Triple &triple,
mike-m79bc57c2010-05-16 19:03:52 +0000767 const HeaderSearchOptions &HSOpts) {
768 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes)
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000769 AddDefaultCPlusPlusIncludePaths(triple);
770
mike-m79bc57c2010-05-16 19:03:52 +0000771 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbare1665822009-11-07 04:20:39 +0000772
773 // Add the default framework include paths on Darwin.
774 if (triple.getOS() == llvm::Triple::Darwin) {
775 AddPath("/System/Library/Frameworks", System, true, false, true);
776 AddPath("/Library/Frameworks", System, true, false, true);
777 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000778}
779
Nico Weber0fca0222008-08-22 09:25:22 +0000780/// RemoveDuplicates - If there are duplicate directory entries in the specified
781/// search list, remove the later (dead) ones.
782static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
783 bool Verbose) {
784 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
785 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
786 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
787 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000788 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Chris Lattner43eee072009-02-08 01:00:10 +0000790 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Chris Lattner43eee072009-02-08 01:00:10 +0000792 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000793 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000794 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000795 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000796 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000797 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000798 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000799 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000800 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000801 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000802 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000803 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000804 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Chris Lattner30f05b52009-02-08 00:55:22 +0000807 // If we have a normal #include dir/framework/headermap that is shadowed
808 // later in the chain by a system include location, we actually want to
809 // ignore the user's request and drop the user dir... keeping the system
810 // dir. This is weird, but required to emulate GCC's search path correctly.
811 //
812 // Since dupes of system dirs are rare, just rescan to find the original
813 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000814 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000815 // Find the dir that this is the same of.
816 unsigned FirstDir;
817 for (FirstDir = 0; ; ++FirstDir) {
818 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Chris Lattner43eee072009-02-08 01:00:10 +0000820 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
821
Chris Lattner30f05b52009-02-08 00:55:22 +0000822 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000823 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000824 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Chris Lattner30f05b52009-02-08 00:55:22 +0000826 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000827 if (CurEntry.isNormalDir())
828 isSame = SearchEntry.getDir() == CurEntry.getDir();
829 else if (CurEntry.isFramework())
830 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000831 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000832 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
833 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000834 }
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Chris Lattner30f05b52009-02-08 00:55:22 +0000836 if (isSame)
837 break;
838 }
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Chris Lattner30f05b52009-02-08 00:55:22 +0000840 // If the first dir in the search path is a non-system dir, zap it
841 // instead of the system one.
842 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
843 DirToRemove = FirstDir;
844 }
845
846 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000847 llvm::errs() << "ignoring duplicate directory \""
848 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +0000849 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000850 llvm::errs() << " as it is a non-system directory that duplicates "
851 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chris Lattner7a739402008-09-26 17:46:45 +0000854 // This is reached if the current entry is a duplicate. Remove the
855 // DirToRemove (usually the current dir).
856 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000857 --i;
858 }
859}
860
861
862void InitHeaderSearch::Realize() {
863 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
864 std::vector<DirectoryLookup> SearchList;
865 SearchList = IncludeGroup[Angled];
866 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
867 IncludeGroup[System].end());
868 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
869 IncludeGroup[After].end());
870 RemoveDuplicates(SearchList, Verbose);
871 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Nico Weber0fca0222008-08-22 09:25:22 +0000873 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000874 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000875 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Nico Weber0fca0222008-08-22 09:25:22 +0000877
878 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
879 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
880 DontSearchCurDir);
881
882 // If verbose, print the list of directories that will be searched.
883 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000884 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000885 unsigned QuotedIdx = IncludeGroup[Quoted].size();
886 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
887 if (i == QuotedIdx)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000888 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000889 const char *Name = SearchList[i].getName();
890 const char *Suffix;
891 if (SearchList[i].isNormalDir())
892 Suffix = "";
893 else if (SearchList[i].isFramework())
894 Suffix = " (framework directory)";
895 else {
896 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
897 Suffix = " (headermap)";
898 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000899 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000900 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000901 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000902 }
903}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000904
Daniel Dunbar5814e652009-11-11 21:44:21 +0000905void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
906 const HeaderSearchOptions &HSOpts,
907 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000908 const llvm::Triple &Triple) {
909 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
910
911 // Add the user defined entries.
912 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
913 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +0000914 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
915 false);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000916 }
917
918 // Add entries from CPATH and friends.
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000919 Init.AddDelimitedPaths(HSOpts.EnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000920 if (Lang.CPlusPlus && Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000921 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000922 else if (Lang.CPlusPlus)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000923 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000924 else if (Lang.ObjC1)
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000925 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000926 else
Benjamin Kramer9e9ddf62009-12-08 12:11:06 +0000927 Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000928
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000929 if (HSOpts.UseStandardIncludes)
mike-m79bc57c2010-05-16 19:03:52 +0000930 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000931
932 Init.Realize();
933}