blob: ed52211e3807e81983e38d1e2bb0aaea2b700439 [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
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000244void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple) {
Mike Stump43d81762009-10-08 23:29:47 +0000245 // FIXME: temporary hack: hard-coded paths.
246 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000247 switch (os) {
248 case llvm::Triple::Win32:
249 {
Mike Stump620d57a2009-10-12 20:50:45 +0000250 std::string VSDir;
251 if (getVisualStudioDir(VSDir)) {
252 AddPath(VSDir + "\\VC\\include", System, false, false, false);
253 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
254 System, false, false, false);
255 }
256 else {
Mike Stump43d81762009-10-08 23:29:47 +0000257 // Default install paths.
Mike Stump620d57a2009-10-12 20:50:45 +0000258 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
Mike Stump43d81762009-10-08 23:29:47 +0000259 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000260 AddPath(
261 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Mike Stump43d81762009-10-08 23:29:47 +0000262 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000263 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
264 System, false, false, false);
265 AddPath(
266 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
267 System, false, false, false);
268 // For some clang developers.
269 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
270 System, false, false, false);
271 AddPath(
272 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
273 System, false, false, false);
274 }
Mike Stump43d81762009-10-08 23:29:47 +0000275 }
276 break;
Mike Stump43d81762009-10-08 23:29:47 +0000277 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000278 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000279 AddPath("c:/mingw/include", System, true, false, false);
280 break;
281 default:
Mike Stump43d81762009-10-08 23:29:47 +0000282 break;
283 }
John Thompsond3f88342009-10-13 18:51:32 +0000284
285 AddPath("/usr/local/include", System, false, false, false);
286 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000287}
288
289void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
290 llvm::Triple::OSType os = triple.getOS();
291 // FIXME: temporary hack: hard-coded paths.
292 switch (os) {
293 case llvm::Triple::Cygwin:
294 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
295 System, true, false, false);
296 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
297 System, true, false, false);
298 break;
299 case llvm::Triple::MinGW64:
300 // Try gcc 4.4.0
301 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
302 // Try gcc 4.3.0
303 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
304 // Fall through.
305 case llvm::Triple::MinGW32:
306 // Try gcc 4.4.0
307 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
308 // Try gcc 4.3.0
309 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
310 break;
311 case llvm::Triple::Darwin:
312 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000313 "i686-apple-darwin10",
314 "i686-apple-darwin10/x86_64",
315 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000316 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
John Thompson40d1bb62009-11-05 22:03:02 +0000317 "i686-apple-darwin8",
318 "i686-apple-darwin8",
319 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000320 break;
321 case llvm::Triple::Linux:
322 // Ubuntu 7.10 - Gutsy Gibbon
323 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000324 "i486-linux-gnu",
325 "i486-linux-gnu",
326 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000327 // Ubuntu 9.04
328 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000329 "x86_64-linux-gnu/32",
330 "x86_64-linux-gnu",
331 triple);
Sebastian Redl5114eca2009-11-05 17:44:49 +0000332 // Ubuntu 9.10
333 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000334 "x86_64-linux-gnu/32",
335 "x86_64-linux-gnu",
336 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000337 // Fedora 8
338 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
John Thompson40d1bb62009-11-05 22:03:02 +0000339 "i386-redhat-linux",
340 "i386-redhat-linux",
341 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000342 // Fedora 9
343 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
John Thompson40d1bb62009-11-05 22:03:02 +0000344 "i386-redhat-linux",
345 "i386-redhat-linux",
346 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000347 // Fedora 10
348 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
John Thompson40d1bb62009-11-05 22:03:02 +0000349 "i386-redhat-linux",
350 "i386-redhat-linux",
351 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000352 // openSUSE 11.1 32 bit
353 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000354 "i586-suse-linux",
355 "i586-suse-linux",
356 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000357 // openSUSE 11.1 64 bit
358 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000359 "x86_64-suse-linux/32",
360 "x86_64-suse-linux",
361 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000362 // openSUSE 11.2
363 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
John Thompson40d1bb62009-11-05 22:03:02 +0000364 "i586-suse-linux",
365 "i586-suse-linux",
366 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000367 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
John Thompson40d1bb62009-11-05 22:03:02 +0000368 "x86_64-suse-linux",
369 "x86_64-suse-linux",
370 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000371 // Arch Linux 2008-06-24
372 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000373 "i686-pc-linux-gnu",
374 "i686-pc-linux-gnu",
375 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000376 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000377 "x86_64-unknown-linux-gnu",
378 "x86_64-unknown-linux-gnu",
379 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000380 // Gentoo x86 2009.1 stable
381 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000382 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
383 "i686-pc-linux-gnu",
384 "i686-pc-linux-gnu",
385 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000386 // Gentoo x86 2009.0 stable
387 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000388 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
389 "i686-pc-linux-gnu",
390 "i686-pc-linux-gnu",
391 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000392 // Gentoo x86 2008.0 stable
393 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000394 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
395 "i686-pc-linux-gnu",
396 "i686-pc-linux-gnu",
397 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000398 // Ubuntu 8.10
399 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000400 "i486-pc-linux-gnu",
401 "i486-pc-linux-gnu",
402 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000403 // Ubuntu 9.04
404 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000405 "i486-linux-gnu",
406 "i486-linux-gnu",
407 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000408 // Gentoo amd64 stable
409 AddGnuCPlusPlusIncludePaths(
410 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
411 "i686-pc-linux-gnu",
412 "i686-pc-linux-gnu",
413 triple);
Benjamin Kramer5d7a1882009-10-30 12:57:13 +0000414 // Exherbo (2009-10-26)
415 AddGnuCPlusPlusIncludePaths(
416 "/usr/include/c++/4.4.2",
417 "x86_64-pc-linux-gnu/32",
418 "x86_64-pc-linux-gnu",
419 triple);
420 AddGnuCPlusPlusIncludePaths(
421 "/usr/include/c++/4.4.2",
422 "i686-pc-linux-gnu",
423 "i686-pc-linux-gnu",
424 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000425 break;
426 case llvm::Triple::FreeBSD:
427 // DragonFly
428 AddPath("/usr/include/c++/4.1", System, true, false, false);
429 // FreeBSD
430 AddPath("/usr/include/c++/4.2", System, true, false, false);
431 break;
432 case llvm::Triple::Solaris:
433 // Solaris - Fall though..
434 case llvm::Triple::AuroraUX:
435 // AuroraUX
436 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
437 "i386-pc-solaris2.11",
438 "i386-pc-solaris2.11",
439 triple);
440 break;
441 default:
442 break;
443 }
444}
445
446void InitHeaderSearch::AddDefaultFrameworkIncludePaths(const llvm::Triple &triple) {
447 llvm::Triple::OSType os = triple.getOS();
448 if (os != llvm::Triple::Darwin)
449 return;
John Thompsond3f88342009-10-13 18:51:32 +0000450 AddPath("/System/Library/Frameworks", System, true, false, true);
451 AddPath("/Library/Frameworks", System, true, false, true);
Nico Weber0fca0222008-08-22 09:25:22 +0000452}
453
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000454void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
455 const llvm::Triple &triple) {
456 AddDefaultCIncludePaths(triple);
457 AddDefaultFrameworkIncludePaths(triple);
458 if (Lang.CPlusPlus)
459 AddDefaultCPlusPlusIncludePaths(triple);
460}
461
Nico Weber0fca0222008-08-22 09:25:22 +0000462void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) {
463 AddEnvVarPaths("CPATH");
464 if (Lang.CPlusPlus && Lang.ObjC1)
465 AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH");
466 else if (Lang.CPlusPlus)
467 AddEnvVarPaths("CPLUS_INCLUDE_PATH");
468 else if (Lang.ObjC1)
469 AddEnvVarPaths("OBJC_INCLUDE_PATH");
470 else
471 AddEnvVarPaths("C_INCLUDE_PATH");
472}
473
474
475/// RemoveDuplicates - If there are duplicate directory entries in the specified
476/// search list, remove the later (dead) ones.
477static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
478 bool Verbose) {
479 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
480 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
481 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
482 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000483 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Chris Lattner43eee072009-02-08 01:00:10 +0000485 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Chris Lattner43eee072009-02-08 01:00:10 +0000487 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000488 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000489 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000490 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000491 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000492 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000493 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000494 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000495 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000496 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000497 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000498 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000499 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattner30f05b52009-02-08 00:55:22 +0000502 // If we have a normal #include dir/framework/headermap that is shadowed
503 // later in the chain by a system include location, we actually want to
504 // ignore the user's request and drop the user dir... keeping the system
505 // dir. This is weird, but required to emulate GCC's search path correctly.
506 //
507 // Since dupes of system dirs are rare, just rescan to find the original
508 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000509 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000510 // Find the dir that this is the same of.
511 unsigned FirstDir;
512 for (FirstDir = 0; ; ++FirstDir) {
513 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattner43eee072009-02-08 01:00:10 +0000515 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
516
Chris Lattner30f05b52009-02-08 00:55:22 +0000517 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000518 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000519 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Chris Lattner30f05b52009-02-08 00:55:22 +0000521 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000522 if (CurEntry.isNormalDir())
523 isSame = SearchEntry.getDir() == CurEntry.getDir();
524 else if (CurEntry.isFramework())
525 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000526 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000527 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
528 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000529 }
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Chris Lattner30f05b52009-02-08 00:55:22 +0000531 if (isSame)
532 break;
533 }
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Chris Lattner30f05b52009-02-08 00:55:22 +0000535 // If the first dir in the search path is a non-system dir, zap it
536 // instead of the system one.
537 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
538 DirToRemove = FirstDir;
539 }
540
541 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000542 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
543 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000544 if (DirToRemove != i)
545 fprintf(stderr, " as it is a non-system directory that duplicates"
546 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000547 }
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Chris Lattner7a739402008-09-26 17:46:45 +0000549 // This is reached if the current entry is a duplicate. Remove the
550 // DirToRemove (usually the current dir).
551 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000552 --i;
553 }
554}
555
556
557void InitHeaderSearch::Realize() {
558 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
559 std::vector<DirectoryLookup> SearchList;
560 SearchList = IncludeGroup[Angled];
561 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
562 IncludeGroup[System].end());
563 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
564 IncludeGroup[After].end());
565 RemoveDuplicates(SearchList, Verbose);
566 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Nico Weber0fca0222008-08-22 09:25:22 +0000568 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000569 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000570 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Nico Weber0fca0222008-08-22 09:25:22 +0000572
573 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
574 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
575 DontSearchCurDir);
576
577 // If verbose, print the list of directories that will be searched.
578 if (Verbose) {
579 fprintf(stderr, "#include \"...\" search starts here:\n");
580 unsigned QuotedIdx = IncludeGroup[Quoted].size();
581 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
582 if (i == QuotedIdx)
583 fprintf(stderr, "#include <...> search starts here:\n");
584 const char *Name = SearchList[i].getName();
585 const char *Suffix;
586 if (SearchList[i].isNormalDir())
587 Suffix = "";
588 else if (SearchList[i].isFramework())
589 Suffix = " (framework directory)";
590 else {
591 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
592 Suffix = " (headermap)";
593 }
594 fprintf(stderr, " %s%s\n", Name, Suffix);
595 }
596 fprintf(stderr, "End of search list.\n");
597 }
598}