blob: 25316bed9fb458bf72a8589026437c8e92e0921d [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
85void InitHeaderSearch::AddEnvVarPaths(const char *Name) {
86 const char* at = getenv(Name);
Daniel Dunbarbb952552008-10-04 20:58:18 +000087 if (!at || *at == 0) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +000088 return;
89
90 const char* delim = strchr(at, llvm::sys::PathSeparator);
91 while (delim != 0) {
92 if (delim-at == 0)
93 AddPath(".", Angled, false, true, false);
94 else
Benjamin Kramer458fb102009-09-05 09:49:39 +000095 AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false);
Nico Weber0fca0222008-08-22 09:25:22 +000096 at = delim + 1;
97 delim = strchr(at, llvm::sys::PathSeparator);
98 }
99 if (*at == 0)
100 AddPath(".", Angled, false, true, false);
101 else
102 AddPath(at, Angled, false, true, false);
103}
104
Mike Stumpec057662009-10-09 20:16:49 +0000105void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(const std::string &Base,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000106 const char *Dir32,
107 const char *Dir64,
108 const llvm::Triple &triple) {
109 llvm::Triple::ArchType arch = triple.getArch();
110 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
111
112 AddPath(Base, System, true, false, false);
113 if (is64bit)
114 AddPath(Base + "/" + Dir64, System, true, false, false);
115 else
116 AddPath(Base + "/" + Dir32, System, true, false, false);
117 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000118}
Nico Weber0fca0222008-08-22 09:25:22 +0000119
Mike Stump620d57a2009-10-12 20:50:45 +0000120void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(const std::string &Base,
121 const char *Arch,
122 const char *Version) {
123 std::string localBase = Base + "/" + Arch + "/" + Version + "/include";
124 AddPath(localBase, System, true, false, false);
125 AddPath(localBase + "/c++", System, true, false, false);
126 AddPath(localBase + "/c++/backward", System, true, false, false);
127}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000128
Mike Stump620d57a2009-10-12 20:50:45 +0000129 // FIXME: This probably should goto to some platform utils place.
130#ifdef _MSC_VER
131 // Read registry string.
132bool getSystemRegistryString(const char *keyPath, const char *valueName,
Mike Stump43d81762009-10-08 23:29:47 +0000133 char *value, size_t maxLength) {
134 HKEY hRootKey = NULL;
135 HKEY hKey = NULL;
136 const char* subKey = NULL;
137 DWORD valueType;
138 DWORD valueSize = maxLength - 1;
139 bool returnValue = false;
140 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
141 hRootKey = HKEY_CLASSES_ROOT;
142 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000143 }
Mike Stump43d81762009-10-08 23:29:47 +0000144 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
145 hRootKey = HKEY_USERS;
146 subKey = keyPath + 11;
147 }
148 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
149 hRootKey = HKEY_LOCAL_MACHINE;
150 subKey = keyPath + 19;
151 }
152 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
153 hRootKey = HKEY_CURRENT_USER;
154 subKey = keyPath + 18;
155 }
156 else
157 return(false);
158 long lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
159 if (lResult == ERROR_SUCCESS) {
Mike Stump620d57a2009-10-12 20:50:45 +0000160 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
161 (LPBYTE)value, &valueSize);
Mike Stump43d81762009-10-08 23:29:47 +0000162 if (lResult == ERROR_SUCCESS)
163 returnValue = true;
Mike Stump620d57a2009-10-12 20:50:45 +0000164 RegCloseKey(hKey);
Mike Stump43d81762009-10-08 23:29:47 +0000165 }
166 return(returnValue);
167}
Mike Stump620d57a2009-10-12 20:50:45 +0000168#else // _MSC_VER
169 // Read registry string.
170bool getSystemRegistryString(const char *, const char *, char *, size_t) {
171 return(false);
172}
173#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000174
Mike Stump43d81762009-10-08 23:29:47 +0000175 // Get Visual Studio installation directory.
176bool getVisualStudioDir(std::string &path) {
Mike Stump620d57a2009-10-12 20:50:45 +0000177 // Try the Windows registry first.
178 char vs80IDEInstallDir[256];
179 char vs90IDEInstallDir[256];
180 const char* vsIDEInstallDir = NULL;
181 bool has80 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000182 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000183 "InstallDir", vs80IDEInstallDir, sizeof(vs80IDEInstallDir) - 1);
184 bool has90 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000185 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000186 "InstallDir", vs90IDEInstallDir, sizeof(vs90IDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000187 // If we have both vc80 and vc90, pick version we were compiled with.
188 if (has80 && has90) {
189 #ifdef _MSC_VER
190 #if (_MSC_VER >= 1500) // VC90
Mike Stump620d57a2009-10-12 20:50:45 +0000191 vsIDEInstallDir = vs90IDEInstallDir;
192 #elif (_MSC_VER == 1400) // VC80
193 vsIDEInstallDir = vs80IDEInstallDir;
194 #else
195 vsIDEInstallDir = vs90IDEInstallDir;
196 #endif
197 #else
198 vsIDEInstallDir = vs90IDEInstallDir;
199 #endif
200 }
201 else if (has90)
202 vsIDEInstallDir = vs90IDEInstallDir;
203 else if (has80)
204 vsIDEInstallDir = vs80IDEInstallDir;
205 if (vsIDEInstallDir && *vsIDEInstallDir) {
206 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
207 if (p)
208 *p = '\0';
209 path = vsIDEInstallDir;
210 return(true);
211 }
212 else {
213 // Try the environment.
214 const char* vs90comntools = getenv("VS90COMNTOOLS");
215 const char* vs80comntools = getenv("VS80COMNTOOLS");
216 const char* vscomntools = NULL;
217 // If we have both vc80 and vc90, pick version we were compiled with.
218 if (vs90comntools && vs80comntools) {
219 #if (_MSC_VER >= 1500) // VC90
Mike Stump43d81762009-10-08 23:29:47 +0000220 vscomntools = vs90comntools;
221 #elif (_MSC_VER == 1400) // VC80
222 vscomntools = vs80comntools;
223 #else
224 vscomntools = vs90comntools;
225 #endif
Mike Stump620d57a2009-10-12 20:50:45 +0000226 }
227 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000228 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000229 else if (vs80comntools)
230 vscomntools = vs80comntools;
231 if (vscomntools && *vscomntools) {
232 char *p = (char*)strstr(vscomntools, "\\Common7\\Tools");
233 if (p)
234 *p = '\0';
235 path = vscomntools;
236 return(true);
237 }
238 else
239 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000240 }
Mike Stump620d57a2009-10-12 20:50:45 +0000241 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000242}
Mike Stump43d81762009-10-08 23:29:47 +0000243
244void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000245 const llvm::Triple &triple) {
Mike Stump43d81762009-10-08 23:29:47 +0000246 // FIXME: temporary hack: hard-coded paths.
247 llvm::Triple::OSType os = triple.getOS();
248
249 switch (os) {
250 case llvm::Triple::Win32:
251 {
Mike Stump620d57a2009-10-12 20:50:45 +0000252 std::string VSDir;
253 if (getVisualStudioDir(VSDir)) {
254 AddPath(VSDir + "\\VC\\include", System, false, false, false);
255 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
256 System, false, false, false);
257 }
258 else {
Mike Stump43d81762009-10-08 23:29:47 +0000259 // Default install paths.
Mike Stump620d57a2009-10-12 20:50:45 +0000260 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
Mike Stump43d81762009-10-08 23:29:47 +0000261 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000262 AddPath(
263 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Mike Stump43d81762009-10-08 23:29:47 +0000264 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000265 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
266 System, false, false, false);
267 AddPath(
268 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
269 System, false, false, false);
270 // For some clang developers.
271 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
272 System, false, false, false);
273 AddPath(
274 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
275 System, false, false, false);
276 }
Mike Stump43d81762009-10-08 23:29:47 +0000277 }
278 break;
279 case llvm::Triple::Cygwin:
280 if (Lang.CPlusPlus) {
Mike Stump620d57a2009-10-12 20:50:45 +0000281 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
282 System, false, false, false);
283 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
284 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000285 }
286 AddPath("/usr/include", System, false, false, false);
287 break;
Mike Stump43d81762009-10-08 23:29:47 +0000288 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000289 if (Lang.CPlusPlus) { // I'm guessing here.
290 // Try gcc 4.4.0
291 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
292 // Try gcc 4.3.0
293 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
294 }
295 // Fall through.
296 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000297 if (Lang.CPlusPlus) {
298 // Try gcc 4.4.0
Mike Stump620d57a2009-10-12 20:50:45 +0000299 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
Mike Stump43d81762009-10-08 23:29:47 +0000300 // Try gcc 4.3.0
Mike Stump620d57a2009-10-12 20:50:45 +0000301 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
Mike Stump43d81762009-10-08 23:29:47 +0000302 }
303 AddPath("c:/mingw/include", System, true, false, false);
304 break;
305 default:
306 if (Lang.CPlusPlus) {
307 switch (os) {
308 case llvm::Triple::Darwin:
309 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000310 "i686-apple-darwin10",
311 "i686-apple-darwin10/x86_64",
312 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000313 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000314 "i686-apple-darwin8",
315 "i686-apple-darwin8",
316 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000317 break;
318 case llvm::Triple::Linux:
319 // Ubuntu 7.10 - Gutsy Gibbon
320 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000321 "i486-linux-gnu",
322 "i486-linux-gnu",
323 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000324 // Ubuntu 9.04
325 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000326 "x86_64-linux-gnu/32",
327 "x86_64-linux-gnu",
328 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000329 // Fedora 8
330 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000331 "i386-redhat-linux",
332 "i386-redhat-linux",
333 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000334 // Fedora 9
335 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000336 "i386-redhat-linux",
337 "i386-redhat-linux",
338 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000339 // Fedora 10
340 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000341 "i386-redhat-linux",
342 "i386-redhat-linux",
343 triple);
344 // openSUSE 11.1 32 bit
Mike Stump43d81762009-10-08 23:29:47 +0000345 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000346 "i586-suse-linux",
347 "i586-suse-linux",
348 triple);
349 // openSUSE 11.1 64 bit
Mike Stump43d81762009-10-08 23:29:47 +0000350 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000351 "x86_64-suse-linux/32",
352 "x86_64-suse-linux",
353 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000354 // openSUSE 11.2
355 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000356 "i586-suse-linux",
357 "i586-suse-linux",
358 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000359 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000360 "x86_64-suse-linux",
361 "x86_64-suse-linux",
362 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000363 // Arch Linux 2008-06-24
364 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000365 "i686-pc-linux-gnu",
366 "i686-pc-linux-gnu",
367 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000368 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000369 "x86_64-unknown-linux-gnu",
370 "x86_64-unknown-linux-gnu",
371 triple);
Nuno Lopesaa546e22009-10-17 23:11:14 +0000372 // Gentoo x86 2009.1 stable
373 AddGnuCPlusPlusIncludePaths(
374 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
375 "i686-pc-linux-gnu",
376 "i686-pc-linux-gnu",
377 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000378 // Gentoo x86 2009.0 stable
379 AddGnuCPlusPlusIncludePaths(
380 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000381 "i686-pc-linux-gnu",
382 "i686-pc-linux-gnu",
383 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000384 // Gentoo x86 2008.0 stable
385 AddGnuCPlusPlusIncludePaths(
386 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000387 "i686-pc-linux-gnu",
388 "i686-pc-linux-gnu",
389 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000390 // Ubuntu 8.10
391 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000392 "i486-pc-linux-gnu",
393 "i486-pc-linux-gnu",
394 triple);
Daniel Dunbarf6dc5ac2009-10-17 03:19:56 +0000395 // Ubuntu 9.04
396 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
397 "i486-linux-gnu",
398 "i486-linux-gnu",
399 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000400 // Gentoo amd64 stable
401 AddGnuCPlusPlusIncludePaths(
402 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000403 "i686-pc-linux-gnu",
404 "i686-pc-linux-gnu",
405 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000406 break;
407 case llvm::Triple::FreeBSD:
408 // DragonFly
409 AddPath("/usr/include/c++/4.1", System, true, false, false);
410 // FreeBSD
411 AddPath("/usr/include/c++/4.2", System, true, false, false);
412 break;
413 case llvm::Triple::Solaris:
Edward O'Callaghan7adf9492009-10-15 07:44:07 +0000414 // Solaris - Fall though..
415 case llvm::Triple::AuroraUX:
Mike Stump43d81762009-10-08 23:29:47 +0000416 // AuroraUX
Edward O'Callaghanff267202009-10-12 12:02:47 +0000417 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000418 "i386-pc-solaris2.11",
419 "i386-pc-solaris2.11",
420 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000421 break;
422 default:
423 break;
424 }
425 }
Mike Stump43d81762009-10-08 23:29:47 +0000426 break;
427 }
John Thompsond3f88342009-10-13 18:51:32 +0000428
429 AddPath("/usr/local/include", System, false, false, false);
430 AddPath("/usr/include", System, false, false, false);
431 AddPath("/System/Library/Frameworks", System, true, false, true);
432 AddPath("/Library/Frameworks", System, true, false, true);
Nico Weber0fca0222008-08-22 09:25:22 +0000433}
434
435void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) {
436 AddEnvVarPaths("CPATH");
437 if (Lang.CPlusPlus && Lang.ObjC1)
438 AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH");
439 else if (Lang.CPlusPlus)
440 AddEnvVarPaths("CPLUS_INCLUDE_PATH");
441 else if (Lang.ObjC1)
442 AddEnvVarPaths("OBJC_INCLUDE_PATH");
443 else
444 AddEnvVarPaths("C_INCLUDE_PATH");
445}
446
447
448/// RemoveDuplicates - If there are duplicate directory entries in the specified
449/// search list, remove the later (dead) ones.
450static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
451 bool Verbose) {
452 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
453 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
454 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
455 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000456 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Chris Lattner43eee072009-02-08 01:00:10 +0000458 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattner43eee072009-02-08 01:00:10 +0000460 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000461 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000462 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000463 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000464 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000465 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000466 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000467 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000468 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000469 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000470 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000471 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000472 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000473 }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Chris Lattner30f05b52009-02-08 00:55:22 +0000475 // If we have a normal #include dir/framework/headermap that is shadowed
476 // later in the chain by a system include location, we actually want to
477 // ignore the user's request and drop the user dir... keeping the system
478 // dir. This is weird, but required to emulate GCC's search path correctly.
479 //
480 // Since dupes of system dirs are rare, just rescan to find the original
481 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000482 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000483 // Find the dir that this is the same of.
484 unsigned FirstDir;
485 for (FirstDir = 0; ; ++FirstDir) {
486 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Chris Lattner43eee072009-02-08 01:00:10 +0000488 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
489
Chris Lattner30f05b52009-02-08 00:55:22 +0000490 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000491 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000492 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattner30f05b52009-02-08 00:55:22 +0000494 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000495 if (CurEntry.isNormalDir())
496 isSame = SearchEntry.getDir() == CurEntry.getDir();
497 else if (CurEntry.isFramework())
498 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000499 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000500 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
501 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Chris Lattner30f05b52009-02-08 00:55:22 +0000504 if (isSame)
505 break;
506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Chris Lattner30f05b52009-02-08 00:55:22 +0000508 // If the first dir in the search path is a non-system dir, zap it
509 // instead of the system one.
510 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
511 DirToRemove = FirstDir;
512 }
513
514 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000515 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
516 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000517 if (DirToRemove != i)
518 fprintf(stderr, " as it is a non-system directory that duplicates"
519 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000520 }
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Chris Lattner7a739402008-09-26 17:46:45 +0000522 // This is reached if the current entry is a duplicate. Remove the
523 // DirToRemove (usually the current dir).
524 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000525 --i;
526 }
527}
528
529
530void InitHeaderSearch::Realize() {
531 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
532 std::vector<DirectoryLookup> SearchList;
533 SearchList = IncludeGroup[Angled];
534 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
535 IncludeGroup[System].end());
536 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
537 IncludeGroup[After].end());
538 RemoveDuplicates(SearchList, Verbose);
539 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Nico Weber0fca0222008-08-22 09:25:22 +0000541 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000542 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000543 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Nico Weber0fca0222008-08-22 09:25:22 +0000545
546 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
547 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
548 DontSearchCurDir);
549
550 // If verbose, print the list of directories that will be searched.
551 if (Verbose) {
552 fprintf(stderr, "#include \"...\" search starts here:\n");
553 unsigned QuotedIdx = IncludeGroup[Quoted].size();
554 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
555 if (i == QuotedIdx)
556 fprintf(stderr, "#include <...> search starts here:\n");
557 const char *Name = SearchList[i].getName();
558 const char *Suffix;
559 if (SearchList[i].isNormalDir())
560 Suffix = "";
561 else if (SearchList[i].isFramework())
562 Suffix = " (framework directory)";
563 else {
564 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
565 Suffix = " (headermap)";
566 }
567 fprintf(stderr, " %s%s\n", Name, Suffix);
568 }
569 fprintf(stderr, "End of search list.\n");
570 }
571}