blob: c9dd3c6d938a59698ec4f52d686d5cfe3bf1ec31 [file] [log] [blame]
Nico Weber0fca0222008-08-22 09:25:22 +00001//===--- InitHeaderSearch.cpp - Initialize header search paths ----------*-===//
2//
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 Espindolaf0a2f512009-11-12 05:48:41 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000022#include "llvm/ADT/Triple.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000023#include "llvm/Support/raw_ostream.h"
Nico Weber0fca0222008-08-22 09:25:22 +000024#include "llvm/System/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000025#include "llvm/Config/config.h"
Chris Lattner3daed522009-03-02 22:20:04 +000026#include <cstdio>
Mike Stump620d57a2009-10-12 20:50:45 +000027#ifdef _MSC_VER
28 #define WIN32_LEAN_AND_MEAN 1
29 #include <windows.h>
30#endif
Nico Weber0fca0222008-08-22 09:25:22 +000031using namespace clang;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000032using namespace clang::frontend;
33
34namespace {
35
36/// InitHeaderSearch - This class makes it easier to set the search paths of
37/// a HeaderSearch object. InitHeaderSearch stores several search path lists
38/// internally, which can be sent to a HeaderSearch object in one swoop.
39class InitHeaderSearch {
40 std::vector<DirectoryLookup> IncludeGroup[4];
41 HeaderSearch& Headers;
42 bool Verbose;
43 std::string isysroot;
44
45public:
46
47 InitHeaderSearch(HeaderSearch &HS,
48 bool verbose = false, const std::string &iSysroot = "")
49 : Headers(HS), Verbose(verbose), isysroot(iSysroot) {}
50
51 /// AddPath - Add the specified path to the specified group list.
52 void AddPath(const llvm::StringRef &Path, IncludeDirGroup Group,
53 bool isCXXAware, bool isUserSupplied,
54 bool isFramework, bool IgnoreSysRoot = false);
55
56 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to suport a gnu
57 /// libstdc++.
58 void AddGnuCPlusPlusIncludePaths(const std::string &Base, const char *Dir32,
59 const char *Dir64,
60 const llvm::Triple &triple);
61
62 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW
63 /// libstdc++.
64 void AddMinGWCPlusPlusIncludePaths(const std::string &Base,
65 const char *Arch,
66 const char *Version);
67
68 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
69 /// separator. The processing follows that of the CPATH variable for gcc.
70 void AddDelimitedPaths(const char *String);
71
72 // AddDefaultCIncludePaths - Add paths that should always be searched.
73 void AddDefaultCIncludePaths(const llvm::Triple &triple);
74
75 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
76 // compiling c++.
77 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
78
79 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
80 /// that e.g. stdio.h is found.
81 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
82 const llvm::Triple &triple);
83
84 /// Realize - Merges all search path lists into one list and send it to
85 /// HeaderSearch.
86 void Realize();
87};
88
89}
Nico Weber0fca0222008-08-22 09:25:22 +000090
Benjamin Kramer458fb102009-09-05 09:49:39 +000091void InitHeaderSearch::AddPath(const llvm::StringRef &Path,
92 IncludeDirGroup Group, bool isCXXAware,
93 bool isUserSupplied, bool isFramework,
94 bool IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000095 assert(!Path.empty() && "can't handle empty path here");
96 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +000097
Nico Weber0fca0222008-08-22 09:25:22 +000098 // Compute the actual path, taking into consideration -isysroot.
99 llvm::SmallString<256> MappedPath;
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Nico Weber0fca0222008-08-22 09:25:22 +0000101 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +0000102 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +0000103 // FIXME: Portability. This should be a sys::Path interface, this doesn't
104 // handle things like C:\ right, nor win32 \\network\device\blah.
105 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
106 MappedPath.append(isysroot.begin(), isysroot.end());
107 }
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Nico Weber0fca0222008-08-22 09:25:22 +0000109 MappedPath.append(Path.begin(), Path.end());
110
111 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000112 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000113 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000114 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000115 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000116 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000117 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000118 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000119
120
Nico Weber0fca0222008-08-22 09:25:22 +0000121 // If the directory exists, add it.
Benjamin Kramer458fb102009-09-05 09:49:39 +0000122 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000123 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
124 isFramework));
125 return;
126 }
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Nico Weber0fca0222008-08-22 09:25:22 +0000128 // Check to see if this is an apple-style headermap (which are not allowed to
129 // be frameworks).
130 if (!isFramework) {
Benjamin Kramer458fb102009-09-05 09:49:39 +0000131 if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000132 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
133 // It is a headermap, add it to the search path.
134 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
135 return;
136 }
137 }
138 }
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Nico Weber0fca0222008-08-22 09:25:22 +0000140 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +0000141 llvm::errs() << "ignoring nonexistent directory \""
142 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000143}
144
145
Daniel Dunbare1665822009-11-07 04:20:39 +0000146void InitHeaderSearch::AddDelimitedPaths(const char *at) {
147 if (*at == 0) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000148 return;
149
150 const char* delim = strchr(at, llvm::sys::PathSeparator);
151 while (delim != 0) {
152 if (delim-at == 0)
153 AddPath(".", Angled, false, true, false);
154 else
Benjamin Kramer458fb102009-09-05 09:49:39 +0000155 AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false);
Nico Weber0fca0222008-08-22 09:25:22 +0000156 at = delim + 1;
157 delim = strchr(at, llvm::sys::PathSeparator);
158 }
159 if (*at == 0)
160 AddPath(".", Angled, false, true, false);
161 else
162 AddPath(at, Angled, false, true, false);
163}
164
Mike Stumpec057662009-10-09 20:16:49 +0000165void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(const std::string &Base,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000166 const char *Dir32,
167 const char *Dir64,
168 const llvm::Triple &triple) {
169 llvm::Triple::ArchType arch = triple.getArch();
170 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
171
172 AddPath(Base, System, true, false, false);
173 if (is64bit)
174 AddPath(Base + "/" + Dir64, System, true, false, false);
175 else
176 AddPath(Base + "/" + Dir32, System, true, false, false);
177 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000178}
Nico Weber0fca0222008-08-22 09:25:22 +0000179
Mike Stump620d57a2009-10-12 20:50:45 +0000180void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(const std::string &Base,
181 const char *Arch,
182 const char *Version) {
Daniel Dunbar5c5758b2009-11-07 04:20:25 +0000183 std::string localBase = Base + "/" + Arch + "/" + Version + "/include";
184 AddPath(localBase, System, true, false, false);
185 AddPath(localBase + "/c++", System, true, false, false);
186 AddPath(localBase + "/c++/backward", System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000187}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000188
Mike Stump620d57a2009-10-12 20:50:45 +0000189 // FIXME: This probably should goto to some platform utils place.
190#ifdef _MSC_VER
191 // Read registry string.
192bool getSystemRegistryString(const char *keyPath, const char *valueName,
Mike Stump43d81762009-10-08 23:29:47 +0000193 char *value, size_t maxLength) {
194 HKEY hRootKey = NULL;
195 HKEY hKey = NULL;
196 const char* subKey = NULL;
197 DWORD valueType;
198 DWORD valueSize = maxLength - 1;
199 bool returnValue = false;
200 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
201 hRootKey = HKEY_CLASSES_ROOT;
202 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000203 }
Mike Stump43d81762009-10-08 23:29:47 +0000204 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
205 hRootKey = HKEY_USERS;
206 subKey = keyPath + 11;
207 }
208 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
209 hRootKey = HKEY_LOCAL_MACHINE;
210 subKey = keyPath + 19;
211 }
212 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
213 hRootKey = HKEY_CURRENT_USER;
214 subKey = keyPath + 18;
215 }
216 else
217 return(false);
218 long lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
219 if (lResult == ERROR_SUCCESS) {
Mike Stump620d57a2009-10-12 20:50:45 +0000220 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
221 (LPBYTE)value, &valueSize);
Mike Stump43d81762009-10-08 23:29:47 +0000222 if (lResult == ERROR_SUCCESS)
223 returnValue = true;
Mike Stump620d57a2009-10-12 20:50:45 +0000224 RegCloseKey(hKey);
Mike Stump43d81762009-10-08 23:29:47 +0000225 }
226 return(returnValue);
227}
Mike Stump620d57a2009-10-12 20:50:45 +0000228#else // _MSC_VER
229 // Read registry string.
230bool getSystemRegistryString(const char *, const char *, char *, size_t) {
231 return(false);
232}
233#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000234
Mike Stump43d81762009-10-08 23:29:47 +0000235 // Get Visual Studio installation directory.
236bool getVisualStudioDir(std::string &path) {
Mike Stump620d57a2009-10-12 20:50:45 +0000237 // Try the Windows registry first.
238 char vs80IDEInstallDir[256];
239 char vs90IDEInstallDir[256];
240 const char* vsIDEInstallDir = NULL;
241 bool has80 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000242 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000243 "InstallDir", vs80IDEInstallDir, sizeof(vs80IDEInstallDir) - 1);
244 bool has90 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000245 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000246 "InstallDir", vs90IDEInstallDir, sizeof(vs90IDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000247 // If we have both vc80 and vc90, pick version we were compiled with.
248 if (has80 && has90) {
249 #ifdef _MSC_VER
250 #if (_MSC_VER >= 1500) // VC90
Mike Stump620d57a2009-10-12 20:50:45 +0000251 vsIDEInstallDir = vs90IDEInstallDir;
252 #elif (_MSC_VER == 1400) // VC80
253 vsIDEInstallDir = vs80IDEInstallDir;
254 #else
255 vsIDEInstallDir = vs90IDEInstallDir;
256 #endif
257 #else
258 vsIDEInstallDir = vs90IDEInstallDir;
259 #endif
260 }
261 else if (has90)
262 vsIDEInstallDir = vs90IDEInstallDir;
263 else if (has80)
264 vsIDEInstallDir = vs80IDEInstallDir;
265 if (vsIDEInstallDir && *vsIDEInstallDir) {
266 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
267 if (p)
268 *p = '\0';
269 path = vsIDEInstallDir;
270 return(true);
271 }
272 else {
273 // Try the environment.
274 const char* vs90comntools = getenv("VS90COMNTOOLS");
275 const char* vs80comntools = getenv("VS80COMNTOOLS");
276 const char* vscomntools = NULL;
277 // If we have both vc80 and vc90, pick version we were compiled with.
278 if (vs90comntools && vs80comntools) {
279 #if (_MSC_VER >= 1500) // VC90
Mike Stump43d81762009-10-08 23:29:47 +0000280 vscomntools = vs90comntools;
281 #elif (_MSC_VER == 1400) // VC80
282 vscomntools = vs80comntools;
283 #else
284 vscomntools = vs90comntools;
285 #endif
Mike Stump620d57a2009-10-12 20:50:45 +0000286 }
287 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000288 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000289 else if (vs80comntools)
290 vscomntools = vs80comntools;
291 if (vscomntools && *vscomntools) {
292 char *p = (char*)strstr(vscomntools, "\\Common7\\Tools");
293 if (p)
294 *p = '\0';
295 path = vscomntools;
296 return(true);
297 }
298 else
299 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000300 }
Mike Stump620d57a2009-10-12 20:50:45 +0000301 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000302}
Mike Stump43d81762009-10-08 23:29:47 +0000303
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000304void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple) {
Mike Stump43d81762009-10-08 23:29:47 +0000305 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000306 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
307 if (CIncludeDirs != "") {
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000308 std::vector<std::string> dirs;
Daniel Dunbarc7064682009-11-12 07:28:29 +0000309 llvm::SplitString(CIncludeDirs, dirs, ":");
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000310 for (std::vector<std::string>::iterator i = dirs.begin(); i != dirs.end(); ++i)
311 AddPath(*i, System, false, false, false);
312 return;
313 }
Mike Stump43d81762009-10-08 23:29:47 +0000314 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000315 switch (os) {
316 case llvm::Triple::Win32:
317 {
Mike Stump620d57a2009-10-12 20:50:45 +0000318 std::string VSDir;
319 if (getVisualStudioDir(VSDir)) {
320 AddPath(VSDir + "\\VC\\include", System, false, false, false);
321 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
322 System, false, false, false);
323 }
324 else {
Mike Stump43d81762009-10-08 23:29:47 +0000325 // Default install paths.
Mike Stump620d57a2009-10-12 20:50:45 +0000326 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
Mike Stump43d81762009-10-08 23:29:47 +0000327 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000328 AddPath(
329 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Mike Stump43d81762009-10-08 23:29:47 +0000330 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000331 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
332 System, false, false, false);
333 AddPath(
334 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
335 System, false, false, false);
336 // For some clang developers.
337 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
338 System, false, false, false);
339 AddPath(
340 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
341 System, false, false, false);
342 }
Mike Stump43d81762009-10-08 23:29:47 +0000343 }
344 break;
Mike Stump43d81762009-10-08 23:29:47 +0000345 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000346 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000347 AddPath("c:/mingw/include", System, true, false, false);
348 break;
349 default:
Mike Stump43d81762009-10-08 23:29:47 +0000350 break;
351 }
John Thompsond3f88342009-10-13 18:51:32 +0000352
353 AddPath("/usr/local/include", System, false, false, false);
354 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000355}
356
357void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
358 llvm::Triple::OSType os = triple.getOS();
359 // FIXME: temporary hack: hard-coded paths.
360 switch (os) {
361 case llvm::Triple::Cygwin:
362 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
363 System, true, false, false);
364 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
365 System, true, false, false);
366 break;
367 case llvm::Triple::MinGW64:
368 // Try gcc 4.4.0
369 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
370 // Try gcc 4.3.0
371 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
372 // Fall through.
373 case llvm::Triple::MinGW32:
374 // Try gcc 4.4.0
375 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
376 // Try gcc 4.3.0
377 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
378 break;
379 case llvm::Triple::Darwin:
380 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000381 "i686-apple-darwin10",
382 "i686-apple-darwin10/x86_64",
383 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000384 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
John Thompson40d1bb62009-11-05 22:03:02 +0000385 "i686-apple-darwin8",
386 "i686-apple-darwin8",
387 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000388 break;
389 case llvm::Triple::Linux:
390 // Ubuntu 7.10 - Gutsy Gibbon
391 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000392 "i486-linux-gnu",
393 "i486-linux-gnu",
394 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000395 // Ubuntu 9.04
396 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000397 "x86_64-linux-gnu/32",
398 "x86_64-linux-gnu",
399 triple);
Sebastian Redl5114eca2009-11-05 17:44:49 +0000400 // Ubuntu 9.10
401 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000402 "x86_64-linux-gnu/32",
403 "x86_64-linux-gnu",
404 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000405 // Fedora 8
406 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
John Thompson40d1bb62009-11-05 22:03:02 +0000407 "i386-redhat-linux",
408 "i386-redhat-linux",
409 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000410 // Fedora 9
411 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
John Thompson40d1bb62009-11-05 22:03:02 +0000412 "i386-redhat-linux",
413 "i386-redhat-linux",
414 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000415 // Fedora 10
416 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
John Thompson40d1bb62009-11-05 22:03:02 +0000417 "i386-redhat-linux",
418 "i386-redhat-linux",
419 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000420 // openSUSE 11.1 32 bit
421 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000422 "i586-suse-linux",
423 "i586-suse-linux",
424 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000425 // openSUSE 11.1 64 bit
426 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000427 "x86_64-suse-linux/32",
428 "x86_64-suse-linux",
429 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000430 // openSUSE 11.2
431 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
John Thompson40d1bb62009-11-05 22:03:02 +0000432 "i586-suse-linux",
433 "i586-suse-linux",
434 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000435 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
John Thompson40d1bb62009-11-05 22:03:02 +0000436 "x86_64-suse-linux",
437 "x86_64-suse-linux",
438 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000439 // Arch Linux 2008-06-24
440 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000441 "i686-pc-linux-gnu",
442 "i686-pc-linux-gnu",
443 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000444 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000445 "x86_64-unknown-linux-gnu",
446 "x86_64-unknown-linux-gnu",
447 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000448 // Gentoo x86 2009.1 stable
449 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000450 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
451 "i686-pc-linux-gnu",
452 "i686-pc-linux-gnu",
453 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000454 // Gentoo x86 2009.0 stable
455 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000456 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
457 "i686-pc-linux-gnu",
458 "i686-pc-linux-gnu",
459 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000460 // Gentoo x86 2008.0 stable
461 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000462 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
463 "i686-pc-linux-gnu",
464 "i686-pc-linux-gnu",
465 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000466 // Ubuntu 8.10
467 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000468 "i486-pc-linux-gnu",
469 "i486-pc-linux-gnu",
470 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000471 // Ubuntu 9.04
472 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000473 "i486-linux-gnu",
474 "i486-linux-gnu",
475 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000476 // Gentoo amd64 stable
477 AddGnuCPlusPlusIncludePaths(
478 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
479 "i686-pc-linux-gnu",
480 "i686-pc-linux-gnu",
481 triple);
Benjamin Kramer5d7a1882009-10-30 12:57:13 +0000482 // Exherbo (2009-10-26)
483 AddGnuCPlusPlusIncludePaths(
484 "/usr/include/c++/4.4.2",
485 "x86_64-pc-linux-gnu/32",
486 "x86_64-pc-linux-gnu",
487 triple);
488 AddGnuCPlusPlusIncludePaths(
489 "/usr/include/c++/4.4.2",
490 "i686-pc-linux-gnu",
491 "i686-pc-linux-gnu",
492 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000493 break;
494 case llvm::Triple::FreeBSD:
495 // DragonFly
496 AddPath("/usr/include/c++/4.1", System, true, false, false);
497 // FreeBSD
498 AddPath("/usr/include/c++/4.2", System, true, false, false);
499 break;
500 case llvm::Triple::Solaris:
501 // Solaris - Fall though..
502 case llvm::Triple::AuroraUX:
503 // AuroraUX
504 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
505 "i386-pc-solaris2.11",
506 "i386-pc-solaris2.11",
507 triple);
508 break;
509 default:
510 break;
511 }
512}
513
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000514void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
515 const llvm::Triple &triple) {
516 AddDefaultCIncludePaths(triple);
Daniel Dunbare1665822009-11-07 04:20:39 +0000517
518 // Add the default framework include paths on Darwin.
519 if (triple.getOS() == llvm::Triple::Darwin) {
520 AddPath("/System/Library/Frameworks", System, true, false, true);
521 AddPath("/Library/Frameworks", System, true, false, true);
522 }
523
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000524 if (Lang.CPlusPlus)
525 AddDefaultCPlusPlusIncludePaths(triple);
526}
527
Nico Weber0fca0222008-08-22 09:25:22 +0000528/// RemoveDuplicates - If there are duplicate directory entries in the specified
529/// search list, remove the later (dead) ones.
530static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
531 bool Verbose) {
532 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
533 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
534 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
535 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000536 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Chris Lattner43eee072009-02-08 01:00:10 +0000538 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Chris Lattner43eee072009-02-08 01:00:10 +0000540 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000541 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000542 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000543 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000544 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000545 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000546 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000547 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000548 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000549 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000550 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000551 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000552 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Chris Lattner30f05b52009-02-08 00:55:22 +0000555 // If we have a normal #include dir/framework/headermap that is shadowed
556 // later in the chain by a system include location, we actually want to
557 // ignore the user's request and drop the user dir... keeping the system
558 // dir. This is weird, but required to emulate GCC's search path correctly.
559 //
560 // Since dupes of system dirs are rare, just rescan to find the original
561 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000562 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000563 // Find the dir that this is the same of.
564 unsigned FirstDir;
565 for (FirstDir = 0; ; ++FirstDir) {
566 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Chris Lattner43eee072009-02-08 01:00:10 +0000568 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
569
Chris Lattner30f05b52009-02-08 00:55:22 +0000570 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000571 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000572 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Chris Lattner30f05b52009-02-08 00:55:22 +0000574 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000575 if (CurEntry.isNormalDir())
576 isSame = SearchEntry.getDir() == CurEntry.getDir();
577 else if (CurEntry.isFramework())
578 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000579 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000580 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
581 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Chris Lattner30f05b52009-02-08 00:55:22 +0000584 if (isSame)
585 break;
586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattner30f05b52009-02-08 00:55:22 +0000588 // If the first dir in the search path is a non-system dir, zap it
589 // instead of the system one.
590 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
591 DirToRemove = FirstDir;
592 }
593
594 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000595 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
596 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000597 if (DirToRemove != i)
598 fprintf(stderr, " as it is a non-system directory that duplicates"
599 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000600 }
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Chris Lattner7a739402008-09-26 17:46:45 +0000602 // This is reached if the current entry is a duplicate. Remove the
603 // DirToRemove (usually the current dir).
604 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000605 --i;
606 }
607}
608
609
610void InitHeaderSearch::Realize() {
611 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
612 std::vector<DirectoryLookup> SearchList;
613 SearchList = IncludeGroup[Angled];
614 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
615 IncludeGroup[System].end());
616 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
617 IncludeGroup[After].end());
618 RemoveDuplicates(SearchList, Verbose);
619 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Nico Weber0fca0222008-08-22 09:25:22 +0000621 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000622 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000623 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Nico Weber0fca0222008-08-22 09:25:22 +0000625
626 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
627 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
628 DontSearchCurDir);
629
630 // If verbose, print the list of directories that will be searched.
631 if (Verbose) {
632 fprintf(stderr, "#include \"...\" search starts here:\n");
633 unsigned QuotedIdx = IncludeGroup[Quoted].size();
634 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
635 if (i == QuotedIdx)
636 fprintf(stderr, "#include <...> search starts here:\n");
637 const char *Name = SearchList[i].getName();
638 const char *Suffix;
639 if (SearchList[i].isNormalDir())
640 Suffix = "";
641 else if (SearchList[i].isFramework())
642 Suffix = " (framework directory)";
643 else {
644 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
645 Suffix = " (headermap)";
646 }
647 fprintf(stderr, " %s%s\n", Name, Suffix);
648 }
649 fprintf(stderr, "End of search list.\n");
650 }
651}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000652
Daniel Dunbar5814e652009-11-11 21:44:21 +0000653void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
654 const HeaderSearchOptions &HSOpts,
655 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000656 const llvm::Triple &Triple) {
657 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
658
659 // Add the user defined entries.
660 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
661 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
662 Init.AddPath(E.Path, E.Group, E.IsCXXAware, E.IsUserSupplied, E.IsFramework,
663 E.IgnoreSysRoot);
664 }
665
666 // Add entries from CPATH and friends.
667 Init.AddDelimitedPaths(HSOpts.EnvIncPath.c_str());
668 Init.AddDelimitedPaths(HSOpts.LangEnvIncPath.c_str());
669
670 if (!HSOpts.BuiltinIncludePath.empty()) {
671 // Ignore the sys root, we *always* look for clang headers relative to
672 // supplied path.
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000673 Init.AddPath(HSOpts.BuiltinIncludePath, System,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000674 false, false, false, /*IgnoreSysRoot=*/ true);
675 }
676
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000677 if (HSOpts.UseStandardIncludes)
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000678 Init.AddDefaultSystemIncludePaths(Lang, Triple);
679
680 Init.Realize();
681}