blob: 822a5baf5972636ac4892795889ddec1b421bb0f [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);
Mike Stump43d81762009-10-08 23:29:47 +0000372 // Gentoo x86 2009.0 stable
373 AddGnuCPlusPlusIncludePaths(
374 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000375 "i686-pc-linux-gnu",
376 "i686-pc-linux-gnu",
377 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000378 // Gentoo x86 2008.0 stable
379 AddGnuCPlusPlusIncludePaths(
380 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.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 // Ubuntu 8.10
385 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000386 "i486-pc-linux-gnu",
387 "i486-pc-linux-gnu",
388 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000389 // Gentoo amd64 stable
390 AddGnuCPlusPlusIncludePaths(
391 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000392 "i686-pc-linux-gnu",
393 "i686-pc-linux-gnu",
394 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000395 break;
396 case llvm::Triple::FreeBSD:
397 // DragonFly
398 AddPath("/usr/include/c++/4.1", System, true, false, false);
399 // FreeBSD
400 AddPath("/usr/include/c++/4.2", System, true, false, false);
401 break;
402 case llvm::Triple::Solaris:
403 // AuroraUX
Edward O'Callaghanff267202009-10-12 12:02:47 +0000404 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindola31b63be2009-10-14 17:09:44 +0000405 "i386-pc-solaris2.11",
406 "i386-pc-solaris2.11",
407 triple);
Mike Stump43d81762009-10-08 23:29:47 +0000408 break;
409 default:
410 break;
411 }
412 }
Mike Stump43d81762009-10-08 23:29:47 +0000413 break;
414 }
John Thompsond3f88342009-10-13 18:51:32 +0000415
416 AddPath("/usr/local/include", System, false, false, false);
417 AddPath("/usr/include", System, false, false, false);
418 AddPath("/System/Library/Frameworks", System, true, false, true);
419 AddPath("/Library/Frameworks", System, true, false, true);
Nico Weber0fca0222008-08-22 09:25:22 +0000420}
421
422void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) {
423 AddEnvVarPaths("CPATH");
424 if (Lang.CPlusPlus && Lang.ObjC1)
425 AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH");
426 else if (Lang.CPlusPlus)
427 AddEnvVarPaths("CPLUS_INCLUDE_PATH");
428 else if (Lang.ObjC1)
429 AddEnvVarPaths("OBJC_INCLUDE_PATH");
430 else
431 AddEnvVarPaths("C_INCLUDE_PATH");
432}
433
434
435/// RemoveDuplicates - If there are duplicate directory entries in the specified
436/// search list, remove the later (dead) ones.
437static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
438 bool Verbose) {
439 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
440 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
441 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
442 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000443 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattner43eee072009-02-08 01:00:10 +0000445 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattner43eee072009-02-08 01:00:10 +0000447 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000448 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000449 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000450 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000451 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000452 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000453 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000454 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000455 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000456 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000457 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000458 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000459 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000460 }
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Chris Lattner30f05b52009-02-08 00:55:22 +0000462 // If we have a normal #include dir/framework/headermap that is shadowed
463 // later in the chain by a system include location, we actually want to
464 // ignore the user's request and drop the user dir... keeping the system
465 // dir. This is weird, but required to emulate GCC's search path correctly.
466 //
467 // Since dupes of system dirs are rare, just rescan to find the original
468 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000469 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000470 // Find the dir that this is the same of.
471 unsigned FirstDir;
472 for (FirstDir = 0; ; ++FirstDir) {
473 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Chris Lattner43eee072009-02-08 01:00:10 +0000475 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
476
Chris Lattner30f05b52009-02-08 00:55:22 +0000477 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000478 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000479 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattner30f05b52009-02-08 00:55:22 +0000481 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000482 if (CurEntry.isNormalDir())
483 isSame = SearchEntry.getDir() == CurEntry.getDir();
484 else if (CurEntry.isFramework())
485 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000486 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000487 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
488 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattner30f05b52009-02-08 00:55:22 +0000491 if (isSame)
492 break;
493 }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattner30f05b52009-02-08 00:55:22 +0000495 // If the first dir in the search path is a non-system dir, zap it
496 // instead of the system one.
497 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
498 DirToRemove = FirstDir;
499 }
500
501 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000502 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
503 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000504 if (DirToRemove != i)
505 fprintf(stderr, " as it is a non-system directory that duplicates"
506 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000507 }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Chris Lattner7a739402008-09-26 17:46:45 +0000509 // This is reached if the current entry is a duplicate. Remove the
510 // DirToRemove (usually the current dir).
511 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000512 --i;
513 }
514}
515
516
517void InitHeaderSearch::Realize() {
518 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
519 std::vector<DirectoryLookup> SearchList;
520 SearchList = IncludeGroup[Angled];
521 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
522 IncludeGroup[System].end());
523 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
524 IncludeGroup[After].end());
525 RemoveDuplicates(SearchList, Verbose);
526 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Nico Weber0fca0222008-08-22 09:25:22 +0000528 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000529 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000530 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Nico Weber0fca0222008-08-22 09:25:22 +0000532
533 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
534 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
535 DontSearchCurDir);
536
537 // If verbose, print the list of directories that will be searched.
538 if (Verbose) {
539 fprintf(stderr, "#include \"...\" search starts here:\n");
540 unsigned QuotedIdx = IncludeGroup[Quoted].size();
541 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
542 if (i == QuotedIdx)
543 fprintf(stderr, "#include <...> search starts here:\n");
544 const char *Name = SearchList[i].getName();
545 const char *Suffix;
546 if (SearchList[i].isNormalDir())
547 Suffix = "";
548 else if (SearchList[i].isFramework())
549 Suffix = " (framework directory)";
550 else {
551 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
552 Suffix = " (headermap)";
553 }
554 fprintf(stderr, " %s%s\n", Name, Suffix);
555 }
556 fprintf(stderr, "End of search list.\n");
557 }
558}