blob: 0ca6e0b830c69fee7bef57463d68bc848bab8f6f [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 Dunbare1bd4e62009-03-02 06:16:29 +000014#include "clang/Frontend/InitHeaderSearch.h"
Nico Weber0fca0222008-08-22 09:25:22 +000015#include "clang/Lex/HeaderSearch.h"
16#include "clang/Basic/FileManager.h"
17#include "clang/Basic/LangOptions.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000020#include "llvm/Support/raw_ostream.h"
Nico Weber0fca0222008-08-22 09:25:22 +000021#include "llvm/System/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000022#include "llvm/Config/config.h"
Chris Lattner3daed522009-03-02 22:20:04 +000023#include <cstdio>
Mike Stump620d57a2009-10-12 20:50:45 +000024#ifdef _MSC_VER
25 #define WIN32_LEAN_AND_MEAN 1
26 #include <windows.h>
27#endif
Nico Weber0fca0222008-08-22 09:25:22 +000028using namespace clang;
29
Benjamin Kramer458fb102009-09-05 09:49:39 +000030void InitHeaderSearch::AddPath(const llvm::StringRef &Path,
31 IncludeDirGroup Group, bool isCXXAware,
32 bool isUserSupplied, bool isFramework,
33 bool IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000034 assert(!Path.empty() && "can't handle empty path here");
35 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +000036
Nico Weber0fca0222008-08-22 09:25:22 +000037 // Compute the actual path, taking into consideration -isysroot.
38 llvm::SmallString<256> MappedPath;
Mike Stump1eb44332009-09-09 15:08:12 +000039
Nico Weber0fca0222008-08-22 09:25:22 +000040 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +000041 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000042 // FIXME: Portability. This should be a sys::Path interface, this doesn't
43 // handle things like C:\ right, nor win32 \\network\device\blah.
44 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
45 MappedPath.append(isysroot.begin(), isysroot.end());
46 }
Mike Stump1eb44332009-09-09 15:08:12 +000047
Nico Weber0fca0222008-08-22 09:25:22 +000048 MappedPath.append(Path.begin(), Path.end());
49
50 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +000051 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +000052 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +000053 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +000054 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +000055 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +000056 else
Chris Lattner0b9e7362008-09-26 21:18:42 +000057 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +000058
59
Nico Weber0fca0222008-08-22 09:25:22 +000060 // If the directory exists, add it.
Benjamin Kramer458fb102009-09-05 09:49:39 +000061 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +000062 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
63 isFramework));
64 return;
65 }
Mike Stump1eb44332009-09-09 15:08:12 +000066
Nico Weber0fca0222008-08-22 09:25:22 +000067 // Check to see if this is an apple-style headermap (which are not allowed to
68 // be frameworks).
69 if (!isFramework) {
Benjamin Kramer458fb102009-09-05 09:49:39 +000070 if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +000071 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
72 // It is a headermap, add it to the search path.
73 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
74 return;
75 }
76 }
77 }
Mike Stump1eb44332009-09-09 15:08:12 +000078
Nico Weber0fca0222008-08-22 09:25:22 +000079 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +000080 llvm::errs() << "ignoring nonexistent directory \""
81 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +000082}
83
84
Daniel Dunbare1665822009-11-07 04:20:39 +000085void InitHeaderSearch::AddDelimitedPaths(const char *at) {
86 if (*at == 0) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +000087 return;
88
89 const char* delim = strchr(at, llvm::sys::PathSeparator);
90 while (delim != 0) {
91 if (delim-at == 0)
92 AddPath(".", Angled, false, true, false);
93 else
Benjamin Kramer458fb102009-09-05 09:49:39 +000094 AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false);
Nico Weber0fca0222008-08-22 09:25:22 +000095 at = delim + 1;
96 delim = strchr(at, llvm::sys::PathSeparator);
97 }
98 if (*at == 0)
99 AddPath(".", Angled, false, true, false);
100 else
101 AddPath(at, Angled, false, true, false);
102}
103
Mike Stumpec057662009-10-09 20:16:49 +0000104void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(const std::string &Base,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000105 const char *Dir32,
106 const char *Dir64,
107 const llvm::Triple &triple) {
108 llvm::Triple::ArchType arch = triple.getArch();
109 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
110
111 AddPath(Base, System, true, false, false);
112 if (is64bit)
113 AddPath(Base + "/" + Dir64, System, true, false, false);
114 else
115 AddPath(Base + "/" + Dir32, System, true, false, false);
116 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000117}
Nico Weber0fca0222008-08-22 09:25:22 +0000118
Mike Stump620d57a2009-10-12 20:50:45 +0000119void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(const std::string &Base,
120 const char *Arch,
121 const char *Version) {
Daniel Dunbar5c5758b2009-11-07 04:20:25 +0000122 std::string localBase = Base + "/" + Arch + "/" + Version + "/include";
123 AddPath(localBase, System, true, false, false);
124 AddPath(localBase + "/c++", System, true, false, false);
125 AddPath(localBase + "/c++/backward", System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000126}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000127
Mike Stump620d57a2009-10-12 20:50:45 +0000128 // FIXME: This probably should goto to some platform utils place.
129#ifdef _MSC_VER
130 // Read registry string.
131bool getSystemRegistryString(const char *keyPath, const char *valueName,
Mike Stump43d81762009-10-08 23:29:47 +0000132 char *value, size_t maxLength) {
133 HKEY hRootKey = NULL;
134 HKEY hKey = NULL;
135 const char* subKey = NULL;
136 DWORD valueType;
137 DWORD valueSize = maxLength - 1;
138 bool returnValue = false;
139 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
140 hRootKey = HKEY_CLASSES_ROOT;
141 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000142 }
Mike Stump43d81762009-10-08 23:29:47 +0000143 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
144 hRootKey = HKEY_USERS;
145 subKey = keyPath + 11;
146 }
147 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
148 hRootKey = HKEY_LOCAL_MACHINE;
149 subKey = keyPath + 19;
150 }
151 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
152 hRootKey = HKEY_CURRENT_USER;
153 subKey = keyPath + 18;
154 }
155 else
156 return(false);
157 long lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
158 if (lResult == ERROR_SUCCESS) {
Mike Stump620d57a2009-10-12 20:50:45 +0000159 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
160 (LPBYTE)value, &valueSize);
Mike Stump43d81762009-10-08 23:29:47 +0000161 if (lResult == ERROR_SUCCESS)
162 returnValue = true;
Mike Stump620d57a2009-10-12 20:50:45 +0000163 RegCloseKey(hKey);
Mike Stump43d81762009-10-08 23:29:47 +0000164 }
165 return(returnValue);
166}
Mike Stump620d57a2009-10-12 20:50:45 +0000167#else // _MSC_VER
168 // Read registry string.
169bool getSystemRegistryString(const char *, const char *, char *, size_t) {
170 return(false);
171}
172#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000173
Mike Stump43d81762009-10-08 23:29:47 +0000174 // Get Visual Studio installation directory.
175bool getVisualStudioDir(std::string &path) {
Mike Stump620d57a2009-10-12 20:50:45 +0000176 // Try the Windows registry first.
177 char vs80IDEInstallDir[256];
178 char vs90IDEInstallDir[256];
179 const char* vsIDEInstallDir = NULL;
180 bool has80 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000181 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000182 "InstallDir", vs80IDEInstallDir, sizeof(vs80IDEInstallDir) - 1);
183 bool has90 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000184 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000185 "InstallDir", vs90IDEInstallDir, sizeof(vs90IDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000186 // If we have both vc80 and vc90, pick version we were compiled with.
187 if (has80 && has90) {
188 #ifdef _MSC_VER
189 #if (_MSC_VER >= 1500) // VC90
Mike Stump620d57a2009-10-12 20:50:45 +0000190 vsIDEInstallDir = vs90IDEInstallDir;
191 #elif (_MSC_VER == 1400) // VC80
192 vsIDEInstallDir = vs80IDEInstallDir;
193 #else
194 vsIDEInstallDir = vs90IDEInstallDir;
195 #endif
196 #else
197 vsIDEInstallDir = vs90IDEInstallDir;
198 #endif
199 }
200 else if (has90)
201 vsIDEInstallDir = vs90IDEInstallDir;
202 else if (has80)
203 vsIDEInstallDir = vs80IDEInstallDir;
204 if (vsIDEInstallDir && *vsIDEInstallDir) {
205 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
206 if (p)
207 *p = '\0';
208 path = vsIDEInstallDir;
209 return(true);
210 }
211 else {
212 // Try the environment.
213 const char* vs90comntools = getenv("VS90COMNTOOLS");
214 const char* vs80comntools = getenv("VS80COMNTOOLS");
215 const char* vscomntools = NULL;
216 // If we have both vc80 and vc90, pick version we were compiled with.
217 if (vs90comntools && vs80comntools) {
218 #if (_MSC_VER >= 1500) // VC90
Mike Stump43d81762009-10-08 23:29:47 +0000219 vscomntools = vs90comntools;
220 #elif (_MSC_VER == 1400) // VC80
221 vscomntools = vs80comntools;
222 #else
223 vscomntools = vs90comntools;
224 #endif
Mike Stump620d57a2009-10-12 20:50:45 +0000225 }
226 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000227 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000228 else if (vs80comntools)
229 vscomntools = vs80comntools;
230 if (vscomntools && *vscomntools) {
231 char *p = (char*)strstr(vscomntools, "\\Common7\\Tools");
232 if (p)
233 *p = '\0';
234 path = vscomntools;
235 return(true);
236 }
237 else
238 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000239 }
Mike Stump620d57a2009-10-12 20:50:45 +0000240 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000241}
Mike Stump43d81762009-10-08 23:29:47 +0000242
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000243void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple) {
Mike Stump43d81762009-10-08 23:29:47 +0000244 // FIXME: temporary hack: hard-coded paths.
245 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000246 switch (os) {
247 case llvm::Triple::Win32:
248 {
Mike Stump620d57a2009-10-12 20:50:45 +0000249 std::string VSDir;
250 if (getVisualStudioDir(VSDir)) {
251 AddPath(VSDir + "\\VC\\include", System, false, false, false);
252 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
253 System, false, false, false);
254 }
255 else {
Mike Stump43d81762009-10-08 23:29:47 +0000256 // Default install paths.
Mike Stump620d57a2009-10-12 20:50:45 +0000257 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
Mike Stump43d81762009-10-08 23:29:47 +0000258 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000259 AddPath(
260 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Mike Stump43d81762009-10-08 23:29:47 +0000261 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000262 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
263 System, false, false, false);
264 AddPath(
265 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
266 System, false, false, false);
267 // For some clang developers.
268 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
269 System, false, false, false);
270 AddPath(
271 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
272 System, false, false, false);
273 }
Mike Stump43d81762009-10-08 23:29:47 +0000274 }
275 break;
Mike Stump43d81762009-10-08 23:29:47 +0000276 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000277 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000278 AddPath("c:/mingw/include", System, true, false, false);
279 break;
280 default:
Mike Stump43d81762009-10-08 23:29:47 +0000281 break;
282 }
John Thompsond3f88342009-10-13 18:51:32 +0000283
284 AddPath("/usr/local/include", System, false, false, false);
285 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000286}
287
288void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
289 llvm::Triple::OSType os = triple.getOS();
290 // FIXME: temporary hack: hard-coded paths.
291 switch (os) {
292 case llvm::Triple::Cygwin:
293 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
294 System, true, false, false);
295 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
296 System, true, false, false);
297 break;
298 case llvm::Triple::MinGW64:
299 // Try gcc 4.4.0
300 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
301 // Try gcc 4.3.0
302 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
303 // Fall through.
304 case llvm::Triple::MinGW32:
305 // Try gcc 4.4.0
306 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
307 // Try gcc 4.3.0
308 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
309 break;
310 case llvm::Triple::Darwin:
311 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000312 "i686-apple-darwin10",
313 "i686-apple-darwin10/x86_64",
314 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000315 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
John Thompson40d1bb62009-11-05 22:03:02 +0000316 "i686-apple-darwin8",
317 "i686-apple-darwin8",
318 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000319 break;
320 case llvm::Triple::Linux:
321 // Ubuntu 7.10 - Gutsy Gibbon
322 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000323 "i486-linux-gnu",
324 "i486-linux-gnu",
325 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000326 // Ubuntu 9.04
327 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000328 "x86_64-linux-gnu/32",
329 "x86_64-linux-gnu",
330 triple);
Sebastian Redl5114eca2009-11-05 17:44:49 +0000331 // Ubuntu 9.10
332 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000333 "x86_64-linux-gnu/32",
334 "x86_64-linux-gnu",
335 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000336 // Fedora 8
337 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
John Thompson40d1bb62009-11-05 22:03:02 +0000338 "i386-redhat-linux",
339 "i386-redhat-linux",
340 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000341 // Fedora 9
342 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
John Thompson40d1bb62009-11-05 22:03:02 +0000343 "i386-redhat-linux",
344 "i386-redhat-linux",
345 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000346 // Fedora 10
347 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
John Thompson40d1bb62009-11-05 22:03:02 +0000348 "i386-redhat-linux",
349 "i386-redhat-linux",
350 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000351 // openSUSE 11.1 32 bit
352 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000353 "i586-suse-linux",
354 "i586-suse-linux",
355 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000356 // openSUSE 11.1 64 bit
357 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000358 "x86_64-suse-linux/32",
359 "x86_64-suse-linux",
360 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000361 // openSUSE 11.2
362 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
John Thompson40d1bb62009-11-05 22:03:02 +0000363 "i586-suse-linux",
364 "i586-suse-linux",
365 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000366 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
John Thompson40d1bb62009-11-05 22:03:02 +0000367 "x86_64-suse-linux",
368 "x86_64-suse-linux",
369 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000370 // Arch Linux 2008-06-24
371 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000372 "i686-pc-linux-gnu",
373 "i686-pc-linux-gnu",
374 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000375 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000376 "x86_64-unknown-linux-gnu",
377 "x86_64-unknown-linux-gnu",
378 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000379 // Gentoo x86 2009.1 stable
380 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000381 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
382 "i686-pc-linux-gnu",
383 "i686-pc-linux-gnu",
384 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000385 // Gentoo x86 2009.0 stable
386 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000387 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
388 "i686-pc-linux-gnu",
389 "i686-pc-linux-gnu",
390 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000391 // Gentoo x86 2008.0 stable
392 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000393 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
394 "i686-pc-linux-gnu",
395 "i686-pc-linux-gnu",
396 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000397 // Ubuntu 8.10
398 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000399 "i486-pc-linux-gnu",
400 "i486-pc-linux-gnu",
401 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000402 // Ubuntu 9.04
403 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000404 "i486-linux-gnu",
405 "i486-linux-gnu",
406 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000407 // Gentoo amd64 stable
408 AddGnuCPlusPlusIncludePaths(
409 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
410 "i686-pc-linux-gnu",
411 "i686-pc-linux-gnu",
412 triple);
Benjamin Kramer5d7a1882009-10-30 12:57:13 +0000413 // Exherbo (2009-10-26)
414 AddGnuCPlusPlusIncludePaths(
415 "/usr/include/c++/4.4.2",
416 "x86_64-pc-linux-gnu/32",
417 "x86_64-pc-linux-gnu",
418 triple);
419 AddGnuCPlusPlusIncludePaths(
420 "/usr/include/c++/4.4.2",
421 "i686-pc-linux-gnu",
422 "i686-pc-linux-gnu",
423 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000424 break;
425 case llvm::Triple::FreeBSD:
426 // DragonFly
427 AddPath("/usr/include/c++/4.1", System, true, false, false);
428 // FreeBSD
429 AddPath("/usr/include/c++/4.2", System, true, false, false);
430 break;
431 case llvm::Triple::Solaris:
432 // Solaris - Fall though..
433 case llvm::Triple::AuroraUX:
434 // AuroraUX
435 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
436 "i386-pc-solaris2.11",
437 "i386-pc-solaris2.11",
438 triple);
439 break;
440 default:
441 break;
442 }
443}
444
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000445void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
446 const llvm::Triple &triple) {
447 AddDefaultCIncludePaths(triple);
Daniel Dunbare1665822009-11-07 04:20:39 +0000448
449 // Add the default framework include paths on Darwin.
450 if (triple.getOS() == llvm::Triple::Darwin) {
451 AddPath("/System/Library/Frameworks", System, true, false, true);
452 AddPath("/Library/Frameworks", System, true, false, true);
453 }
454
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000455 if (Lang.CPlusPlus)
456 AddDefaultCPlusPlusIncludePaths(triple);
457}
458
Nico Weber0fca0222008-08-22 09:25:22 +0000459/// RemoveDuplicates - If there are duplicate directory entries in the specified
460/// search list, remove the later (dead) ones.
461static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
462 bool Verbose) {
463 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
464 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
465 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
466 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000467 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Chris Lattner43eee072009-02-08 01:00:10 +0000469 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattner43eee072009-02-08 01:00:10 +0000471 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000472 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000473 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000474 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000475 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000476 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000477 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000478 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000479 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000480 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000481 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000482 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000483 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattner30f05b52009-02-08 00:55:22 +0000486 // If we have a normal #include dir/framework/headermap that is shadowed
487 // later in the chain by a system include location, we actually want to
488 // ignore the user's request and drop the user dir... keeping the system
489 // dir. This is weird, but required to emulate GCC's search path correctly.
490 //
491 // Since dupes of system dirs are rare, just rescan to find the original
492 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000493 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000494 // Find the dir that this is the same of.
495 unsigned FirstDir;
496 for (FirstDir = 0; ; ++FirstDir) {
497 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Chris Lattner43eee072009-02-08 01:00:10 +0000499 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
500
Chris Lattner30f05b52009-02-08 00:55:22 +0000501 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000502 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000503 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Chris Lattner30f05b52009-02-08 00:55:22 +0000505 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000506 if (CurEntry.isNormalDir())
507 isSame = SearchEntry.getDir() == CurEntry.getDir();
508 else if (CurEntry.isFramework())
509 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000510 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000511 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
512 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattner30f05b52009-02-08 00:55:22 +0000515 if (isSame)
516 break;
517 }
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chris Lattner30f05b52009-02-08 00:55:22 +0000519 // If the first dir in the search path is a non-system dir, zap it
520 // instead of the system one.
521 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
522 DirToRemove = FirstDir;
523 }
524
525 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000526 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
527 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000528 if (DirToRemove != i)
529 fprintf(stderr, " as it is a non-system directory that duplicates"
530 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattner7a739402008-09-26 17:46:45 +0000533 // This is reached if the current entry is a duplicate. Remove the
534 // DirToRemove (usually the current dir).
535 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000536 --i;
537 }
538}
539
540
541void InitHeaderSearch::Realize() {
542 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
543 std::vector<DirectoryLookup> SearchList;
544 SearchList = IncludeGroup[Angled];
545 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
546 IncludeGroup[System].end());
547 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
548 IncludeGroup[After].end());
549 RemoveDuplicates(SearchList, Verbose);
550 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Nico Weber0fca0222008-08-22 09:25:22 +0000552 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000553 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000554 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Nico Weber0fca0222008-08-22 09:25:22 +0000556
557 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
558 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
559 DontSearchCurDir);
560
561 // If verbose, print the list of directories that will be searched.
562 if (Verbose) {
563 fprintf(stderr, "#include \"...\" search starts here:\n");
564 unsigned QuotedIdx = IncludeGroup[Quoted].size();
565 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
566 if (i == QuotedIdx)
567 fprintf(stderr, "#include <...> search starts here:\n");
568 const char *Name = SearchList[i].getName();
569 const char *Suffix;
570 if (SearchList[i].isNormalDir())
571 Suffix = "";
572 else if (SearchList[i].isFramework())
573 Suffix = " (framework directory)";
574 else {
575 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
576 Suffix = " (headermap)";
577 }
578 fprintf(stderr, " %s%s\n", Name, Suffix);
579 }
580 fprintf(stderr, "End of search list.\n");
581 }
582}