blob: 989829895d3a5e9a938f7a36b7d64484fa6d1389 [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/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"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000021#include "llvm/Support/raw_ostream.h"
Nico Weber0fca0222008-08-22 09:25:22 +000022#include "llvm/System/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000023#include "llvm/Config/config.h"
Chris Lattner3daed522009-03-02 22:20:04 +000024#include <cstdio>
Mike Stump620d57a2009-10-12 20:50:45 +000025#ifdef _MSC_VER
26 #define WIN32_LEAN_AND_MEAN 1
27 #include <windows.h>
28#endif
Nico Weber0fca0222008-08-22 09:25:22 +000029using namespace clang;
30
Benjamin Kramer458fb102009-09-05 09:49:39 +000031void InitHeaderSearch::AddPath(const llvm::StringRef &Path,
32 IncludeDirGroup Group, bool isCXXAware,
33 bool isUserSupplied, bool isFramework,
34 bool IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000035 assert(!Path.empty() && "can't handle empty path here");
36 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +000037
Nico Weber0fca0222008-08-22 09:25:22 +000038 // Compute the actual path, taking into consideration -isysroot.
39 llvm::SmallString<256> MappedPath;
Mike Stump1eb44332009-09-09 15:08:12 +000040
Nico Weber0fca0222008-08-22 09:25:22 +000041 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +000042 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000043 // FIXME: Portability. This should be a sys::Path interface, this doesn't
44 // handle things like C:\ right, nor win32 \\network\device\blah.
45 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
46 MappedPath.append(isysroot.begin(), isysroot.end());
47 }
Mike Stump1eb44332009-09-09 15:08:12 +000048
Nico Weber0fca0222008-08-22 09:25:22 +000049 MappedPath.append(Path.begin(), Path.end());
50
51 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +000052 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +000053 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +000054 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +000055 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +000056 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +000057 else
Chris Lattner0b9e7362008-09-26 21:18:42 +000058 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +000059
60
Nico Weber0fca0222008-08-22 09:25:22 +000061 // If the directory exists, add it.
Benjamin Kramer458fb102009-09-05 09:49:39 +000062 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +000063 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
64 isFramework));
65 return;
66 }
Mike Stump1eb44332009-09-09 15:08:12 +000067
Nico Weber0fca0222008-08-22 09:25:22 +000068 // Check to see if this is an apple-style headermap (which are not allowed to
69 // be frameworks).
70 if (!isFramework) {
Benjamin Kramer458fb102009-09-05 09:49:39 +000071 if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +000072 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
73 // It is a headermap, add it to the search path.
74 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
75 return;
76 }
77 }
78 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Nico Weber0fca0222008-08-22 09:25:22 +000080 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +000081 llvm::errs() << "ignoring nonexistent directory \""
82 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +000083}
84
85
Daniel Dunbare1665822009-11-07 04:20:39 +000086void InitHeaderSearch::AddDelimitedPaths(const char *at) {
87 if (*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) {
Daniel Dunbar5c5758b2009-11-07 04:20:25 +0000123 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);
Mike Stump620d57a2009-10-12 20:50:45 +0000127}
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
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000446void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
447 const llvm::Triple &triple) {
448 AddDefaultCIncludePaths(triple);
Daniel Dunbare1665822009-11-07 04:20:39 +0000449
450 // Add the default framework include paths on Darwin.
451 if (triple.getOS() == llvm::Triple::Darwin) {
452 AddPath("/System/Library/Frameworks", System, true, false, true);
453 AddPath("/Library/Frameworks", System, true, false, true);
454 }
455
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000456 if (Lang.CPlusPlus)
457 AddDefaultCPlusPlusIncludePaths(triple);
458}
459
Nico Weber0fca0222008-08-22 09:25:22 +0000460/// RemoveDuplicates - If there are duplicate directory entries in the specified
461/// search list, remove the later (dead) ones.
462static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
463 bool Verbose) {
464 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
465 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
466 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
467 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000468 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Chris Lattner43eee072009-02-08 01:00:10 +0000470 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Chris Lattner43eee072009-02-08 01:00:10 +0000472 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000473 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000474 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000475 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000476 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000477 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000478 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000479 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000480 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000481 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000482 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000483 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000484 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Chris Lattner30f05b52009-02-08 00:55:22 +0000487 // If we have a normal #include dir/framework/headermap that is shadowed
488 // later in the chain by a system include location, we actually want to
489 // ignore the user's request and drop the user dir... keeping the system
490 // dir. This is weird, but required to emulate GCC's search path correctly.
491 //
492 // Since dupes of system dirs are rare, just rescan to find the original
493 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000494 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000495 // Find the dir that this is the same of.
496 unsigned FirstDir;
497 for (FirstDir = 0; ; ++FirstDir) {
498 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Chris Lattner43eee072009-02-08 01:00:10 +0000500 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
501
Chris Lattner30f05b52009-02-08 00:55:22 +0000502 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000503 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000504 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Chris Lattner30f05b52009-02-08 00:55:22 +0000506 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000507 if (CurEntry.isNormalDir())
508 isSame = SearchEntry.getDir() == CurEntry.getDir();
509 else if (CurEntry.isFramework())
510 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000511 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000512 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
513 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Chris Lattner30f05b52009-02-08 00:55:22 +0000516 if (isSame)
517 break;
518 }
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Chris Lattner30f05b52009-02-08 00:55:22 +0000520 // If the first dir in the search path is a non-system dir, zap it
521 // instead of the system one.
522 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
523 DirToRemove = FirstDir;
524 }
525
526 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000527 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
528 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000529 if (DirToRemove != i)
530 fprintf(stderr, " as it is a non-system directory that duplicates"
531 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000532 }
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Chris Lattner7a739402008-09-26 17:46:45 +0000534 // This is reached if the current entry is a duplicate. Remove the
535 // DirToRemove (usually the current dir).
536 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000537 --i;
538 }
539}
540
541
542void InitHeaderSearch::Realize() {
543 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
544 std::vector<DirectoryLookup> SearchList;
545 SearchList = IncludeGroup[Angled];
546 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
547 IncludeGroup[System].end());
548 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
549 IncludeGroup[After].end());
550 RemoveDuplicates(SearchList, Verbose);
551 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Nico Weber0fca0222008-08-22 09:25:22 +0000553 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000554 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000555 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Nico Weber0fca0222008-08-22 09:25:22 +0000557
558 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
559 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
560 DontSearchCurDir);
561
562 // If verbose, print the list of directories that will be searched.
563 if (Verbose) {
564 fprintf(stderr, "#include \"...\" search starts here:\n");
565 unsigned QuotedIdx = IncludeGroup[Quoted].size();
566 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
567 if (i == QuotedIdx)
568 fprintf(stderr, "#include <...> search starts here:\n");
569 const char *Name = SearchList[i].getName();
570 const char *Suffix;
571 if (SearchList[i].isNormalDir())
572 Suffix = "";
573 else if (SearchList[i].isFramework())
574 Suffix = " (framework directory)";
575 else {
576 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
577 Suffix = " (headermap)";
578 }
579 fprintf(stderr, " %s%s\n", Name, Suffix);
580 }
581 fprintf(stderr, "End of search list.\n");
582 }
583}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000584
585void clang::ApplyHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
586 HeaderSearch &HS, const LangOptions &Lang,
587 const llvm::Triple &Triple) {
588 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
589
590 // Add the user defined entries.
591 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
592 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
593 Init.AddPath(E.Path, E.Group, E.IsCXXAware, E.IsUserSupplied, E.IsFramework,
594 E.IgnoreSysRoot);
595 }
596
597 // Add entries from CPATH and friends.
598 Init.AddDelimitedPaths(HSOpts.EnvIncPath.c_str());
599 Init.AddDelimitedPaths(HSOpts.LangEnvIncPath.c_str());
600
601 if (!HSOpts.BuiltinIncludePath.empty()) {
602 // Ignore the sys root, we *always* look for clang headers relative to
603 // supplied path.
604 Init.AddPath(HSOpts.BuiltinIncludePath, InitHeaderSearch::System,
605 false, false, false, /*IgnoreSysRoot=*/ true);
606 }
607
608 if (!HSOpts.UseStandardIncludes)
609 Init.AddDefaultSystemIncludePaths(Lang, Triple);
610
611 Init.Realize();
612}