blob: b4f48ac8fde1aabb0b7c306fa549fc419c1b3b61 [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 Dunbar2cdafa82009-11-09 23:02:47 +000014#include "clang/Frontend/Utils.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"
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000021#include "llvm/ADT/Triple.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000022#include "llvm/Support/raw_ostream.h"
Nico Weber0fca0222008-08-22 09:25:22 +000023#include "llvm/System/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000024#include "llvm/Config/config.h"
Chris Lattner3daed522009-03-02 22:20:04 +000025#include <cstdio>
Mike Stump620d57a2009-10-12 20:50:45 +000026#ifdef _MSC_VER
27 #define WIN32_LEAN_AND_MEAN 1
28 #include <windows.h>
29#endif
Nico Weber0fca0222008-08-22 09:25:22 +000030using namespace clang;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000031using namespace clang::frontend;
32
33namespace {
34
35/// InitHeaderSearch - This class makes it easier to set the search paths of
36/// a HeaderSearch object. InitHeaderSearch stores several search path lists
37/// internally, which can be sent to a HeaderSearch object in one swoop.
38class InitHeaderSearch {
39 std::vector<DirectoryLookup> IncludeGroup[4];
40 HeaderSearch& Headers;
41 bool Verbose;
42 std::string isysroot;
43
44public:
45
46 InitHeaderSearch(HeaderSearch &HS,
47 bool verbose = false, const std::string &iSysroot = "")
48 : Headers(HS), Verbose(verbose), isysroot(iSysroot) {}
49
50 /// AddPath - Add the specified path to the specified group list.
51 void AddPath(const llvm::StringRef &Path, IncludeDirGroup Group,
52 bool isCXXAware, bool isUserSupplied,
53 bool isFramework, bool IgnoreSysRoot = false);
54
55 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to suport a gnu
56 /// libstdc++.
57 void AddGnuCPlusPlusIncludePaths(const std::string &Base, const char *Dir32,
58 const char *Dir64,
59 const llvm::Triple &triple);
60
61 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW
62 /// libstdc++.
63 void AddMinGWCPlusPlusIncludePaths(const std::string &Base,
64 const char *Arch,
65 const char *Version);
66
67 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
68 /// separator. The processing follows that of the CPATH variable for gcc.
69 void AddDelimitedPaths(const char *String);
70
71 // AddDefaultCIncludePaths - Add paths that should always be searched.
72 void AddDefaultCIncludePaths(const llvm::Triple &triple);
73
74 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
75 // compiling c++.
76 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
77
78 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
79 /// that e.g. stdio.h is found.
80 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
81 const llvm::Triple &triple);
82
83 /// Realize - Merges all search path lists into one list and send it to
84 /// HeaderSearch.
85 void Realize();
86};
87
88}
Nico Weber0fca0222008-08-22 09:25:22 +000089
Benjamin Kramer458fb102009-09-05 09:49:39 +000090void InitHeaderSearch::AddPath(const llvm::StringRef &Path,
91 IncludeDirGroup Group, bool isCXXAware,
92 bool isUserSupplied, bool isFramework,
93 bool IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000094 assert(!Path.empty() && "can't handle empty path here");
95 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +000096
Nico Weber0fca0222008-08-22 09:25:22 +000097 // Compute the actual path, taking into consideration -isysroot.
98 llvm::SmallString<256> MappedPath;
Mike Stump1eb44332009-09-09 15:08:12 +000099
Nico Weber0fca0222008-08-22 09:25:22 +0000100 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +0000101 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +0000102 // FIXME: Portability. This should be a sys::Path interface, this doesn't
103 // handle things like C:\ right, nor win32 \\network\device\blah.
104 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
105 MappedPath.append(isysroot.begin(), isysroot.end());
106 }
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Nico Weber0fca0222008-08-22 09:25:22 +0000108 MappedPath.append(Path.begin(), Path.end());
109
110 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000111 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000112 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000113 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000114 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000115 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000116 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000117 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000118
119
Nico Weber0fca0222008-08-22 09:25:22 +0000120 // If the directory exists, add it.
Benjamin Kramer458fb102009-09-05 09:49:39 +0000121 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000122 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
123 isFramework));
124 return;
125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Nico Weber0fca0222008-08-22 09:25:22 +0000127 // Check to see if this is an apple-style headermap (which are not allowed to
128 // be frameworks).
129 if (!isFramework) {
Benjamin Kramer458fb102009-09-05 09:49:39 +0000130 if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000131 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
132 // It is a headermap, add it to the search path.
133 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
134 return;
135 }
136 }
137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Nico Weber0fca0222008-08-22 09:25:22 +0000139 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +0000140 llvm::errs() << "ignoring nonexistent directory \""
141 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000142}
143
144
Daniel Dunbare1665822009-11-07 04:20:39 +0000145void InitHeaderSearch::AddDelimitedPaths(const char *at) {
146 if (*at == 0) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000147 return;
148
149 const char* delim = strchr(at, llvm::sys::PathSeparator);
150 while (delim != 0) {
151 if (delim-at == 0)
152 AddPath(".", Angled, false, true, false);
153 else
Benjamin Kramer458fb102009-09-05 09:49:39 +0000154 AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false);
Nico Weber0fca0222008-08-22 09:25:22 +0000155 at = delim + 1;
156 delim = strchr(at, llvm::sys::PathSeparator);
157 }
158 if (*at == 0)
159 AddPath(".", Angled, false, true, false);
160 else
161 AddPath(at, Angled, false, true, false);
162}
163
Mike Stumpec057662009-10-09 20:16:49 +0000164void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(const std::string &Base,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000165 const char *Dir32,
166 const char *Dir64,
167 const llvm::Triple &triple) {
168 llvm::Triple::ArchType arch = triple.getArch();
169 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
170
171 AddPath(Base, System, true, false, false);
172 if (is64bit)
173 AddPath(Base + "/" + Dir64, System, true, false, false);
174 else
175 AddPath(Base + "/" + Dir32, System, true, false, false);
176 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000177}
Nico Weber0fca0222008-08-22 09:25:22 +0000178
Mike Stump620d57a2009-10-12 20:50:45 +0000179void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(const std::string &Base,
180 const char *Arch,
181 const char *Version) {
Daniel Dunbar5c5758b2009-11-07 04:20:25 +0000182 std::string localBase = Base + "/" + Arch + "/" + Version + "/include";
183 AddPath(localBase, System, true, false, false);
184 AddPath(localBase + "/c++", System, true, false, false);
185 AddPath(localBase + "/c++/backward", System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000186}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000187
Mike Stump620d57a2009-10-12 20:50:45 +0000188 // FIXME: This probably should goto to some platform utils place.
189#ifdef _MSC_VER
190 // Read registry string.
191bool getSystemRegistryString(const char *keyPath, const char *valueName,
Mike Stump43d81762009-10-08 23:29:47 +0000192 char *value, size_t maxLength) {
193 HKEY hRootKey = NULL;
194 HKEY hKey = NULL;
195 const char* subKey = NULL;
196 DWORD valueType;
197 DWORD valueSize = maxLength - 1;
198 bool returnValue = false;
199 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
200 hRootKey = HKEY_CLASSES_ROOT;
201 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000202 }
Mike Stump43d81762009-10-08 23:29:47 +0000203 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
204 hRootKey = HKEY_USERS;
205 subKey = keyPath + 11;
206 }
207 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
208 hRootKey = HKEY_LOCAL_MACHINE;
209 subKey = keyPath + 19;
210 }
211 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
212 hRootKey = HKEY_CURRENT_USER;
213 subKey = keyPath + 18;
214 }
215 else
216 return(false);
217 long lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
218 if (lResult == ERROR_SUCCESS) {
Mike Stump620d57a2009-10-12 20:50:45 +0000219 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
220 (LPBYTE)value, &valueSize);
Mike Stump43d81762009-10-08 23:29:47 +0000221 if (lResult == ERROR_SUCCESS)
222 returnValue = true;
Mike Stump620d57a2009-10-12 20:50:45 +0000223 RegCloseKey(hKey);
Mike Stump43d81762009-10-08 23:29:47 +0000224 }
225 return(returnValue);
226}
Mike Stump620d57a2009-10-12 20:50:45 +0000227#else // _MSC_VER
228 // Read registry string.
229bool getSystemRegistryString(const char *, const char *, char *, size_t) {
230 return(false);
231}
232#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000233
Mike Stump43d81762009-10-08 23:29:47 +0000234 // Get Visual Studio installation directory.
235bool getVisualStudioDir(std::string &path) {
Mike Stump620d57a2009-10-12 20:50:45 +0000236 // Try the Windows registry first.
237 char vs80IDEInstallDir[256];
238 char vs90IDEInstallDir[256];
239 const char* vsIDEInstallDir = NULL;
240 bool has80 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000241 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000242 "InstallDir", vs80IDEInstallDir, sizeof(vs80IDEInstallDir) - 1);
243 bool has90 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000244 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000245 "InstallDir", vs90IDEInstallDir, sizeof(vs90IDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000246 // If we have both vc80 and vc90, pick version we were compiled with.
247 if (has80 && has90) {
248 #ifdef _MSC_VER
249 #if (_MSC_VER >= 1500) // VC90
Mike Stump620d57a2009-10-12 20:50:45 +0000250 vsIDEInstallDir = vs90IDEInstallDir;
251 #elif (_MSC_VER == 1400) // VC80
252 vsIDEInstallDir = vs80IDEInstallDir;
253 #else
254 vsIDEInstallDir = vs90IDEInstallDir;
255 #endif
256 #else
257 vsIDEInstallDir = vs90IDEInstallDir;
258 #endif
259 }
260 else if (has90)
261 vsIDEInstallDir = vs90IDEInstallDir;
262 else if (has80)
263 vsIDEInstallDir = vs80IDEInstallDir;
264 if (vsIDEInstallDir && *vsIDEInstallDir) {
265 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
266 if (p)
267 *p = '\0';
268 path = vsIDEInstallDir;
269 return(true);
270 }
271 else {
272 // Try the environment.
273 const char* vs90comntools = getenv("VS90COMNTOOLS");
274 const char* vs80comntools = getenv("VS80COMNTOOLS");
275 const char* vscomntools = NULL;
276 // If we have both vc80 and vc90, pick version we were compiled with.
277 if (vs90comntools && vs80comntools) {
278 #if (_MSC_VER >= 1500) // VC90
Mike Stump43d81762009-10-08 23:29:47 +0000279 vscomntools = vs90comntools;
280 #elif (_MSC_VER == 1400) // VC80
281 vscomntools = vs80comntools;
282 #else
283 vscomntools = vs90comntools;
284 #endif
Mike Stump620d57a2009-10-12 20:50:45 +0000285 }
286 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000287 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000288 else if (vs80comntools)
289 vscomntools = vs80comntools;
290 if (vscomntools && *vscomntools) {
291 char *p = (char*)strstr(vscomntools, "\\Common7\\Tools");
292 if (p)
293 *p = '\0';
294 path = vscomntools;
295 return(true);
296 }
297 else
298 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000299 }
Mike Stump620d57a2009-10-12 20:50:45 +0000300 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000301}
Mike Stump43d81762009-10-08 23:29:47 +0000302
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000303void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple) {
Mike Stump43d81762009-10-08 23:29:47 +0000304 // FIXME: temporary hack: hard-coded paths.
305 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000306 switch (os) {
307 case llvm::Triple::Win32:
308 {
Mike Stump620d57a2009-10-12 20:50:45 +0000309 std::string VSDir;
310 if (getVisualStudioDir(VSDir)) {
311 AddPath(VSDir + "\\VC\\include", System, false, false, false);
312 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
313 System, false, false, false);
314 }
315 else {
Mike Stump43d81762009-10-08 23:29:47 +0000316 // Default install paths.
Mike Stump620d57a2009-10-12 20:50:45 +0000317 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
Mike Stump43d81762009-10-08 23:29:47 +0000318 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000319 AddPath(
320 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Mike Stump43d81762009-10-08 23:29:47 +0000321 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000322 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
323 System, false, false, false);
324 AddPath(
325 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
326 System, false, false, false);
327 // For some clang developers.
328 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
329 System, false, false, false);
330 AddPath(
331 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
332 System, false, false, false);
333 }
Mike Stump43d81762009-10-08 23:29:47 +0000334 }
335 break;
Mike Stump43d81762009-10-08 23:29:47 +0000336 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000337 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000338 AddPath("c:/mingw/include", System, true, false, false);
339 break;
340 default:
Mike Stump43d81762009-10-08 23:29:47 +0000341 break;
342 }
John Thompsond3f88342009-10-13 18:51:32 +0000343
344 AddPath("/usr/local/include", System, false, false, false);
345 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000346}
347
348void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
349 llvm::Triple::OSType os = triple.getOS();
350 // FIXME: temporary hack: hard-coded paths.
351 switch (os) {
352 case llvm::Triple::Cygwin:
353 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
354 System, true, false, false);
355 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
356 System, true, false, false);
357 break;
358 case llvm::Triple::MinGW64:
359 // Try gcc 4.4.0
360 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
361 // Try gcc 4.3.0
362 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
363 // Fall through.
364 case llvm::Triple::MinGW32:
365 // Try gcc 4.4.0
366 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
367 // Try gcc 4.3.0
368 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
369 break;
370 case llvm::Triple::Darwin:
371 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000372 "i686-apple-darwin10",
373 "i686-apple-darwin10/x86_64",
374 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000375 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
John Thompson40d1bb62009-11-05 22:03:02 +0000376 "i686-apple-darwin8",
377 "i686-apple-darwin8",
378 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000379 break;
380 case llvm::Triple::Linux:
381 // Ubuntu 7.10 - Gutsy Gibbon
382 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000383 "i486-linux-gnu",
384 "i486-linux-gnu",
385 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000386 // Ubuntu 9.04
387 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000388 "x86_64-linux-gnu/32",
389 "x86_64-linux-gnu",
390 triple);
Sebastian Redl5114eca2009-11-05 17:44:49 +0000391 // Ubuntu 9.10
392 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000393 "x86_64-linux-gnu/32",
394 "x86_64-linux-gnu",
395 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000396 // Fedora 8
397 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
John Thompson40d1bb62009-11-05 22:03:02 +0000398 "i386-redhat-linux",
399 "i386-redhat-linux",
400 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000401 // Fedora 9
402 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
John Thompson40d1bb62009-11-05 22:03:02 +0000403 "i386-redhat-linux",
404 "i386-redhat-linux",
405 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000406 // Fedora 10
407 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
John Thompson40d1bb62009-11-05 22:03:02 +0000408 "i386-redhat-linux",
409 "i386-redhat-linux",
410 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000411 // openSUSE 11.1 32 bit
412 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000413 "i586-suse-linux",
414 "i586-suse-linux",
415 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000416 // openSUSE 11.1 64 bit
417 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000418 "x86_64-suse-linux/32",
419 "x86_64-suse-linux",
420 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000421 // openSUSE 11.2
422 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
John Thompson40d1bb62009-11-05 22:03:02 +0000423 "i586-suse-linux",
424 "i586-suse-linux",
425 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000426 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
John Thompson40d1bb62009-11-05 22:03:02 +0000427 "x86_64-suse-linux",
428 "x86_64-suse-linux",
429 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000430 // Arch Linux 2008-06-24
431 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000432 "i686-pc-linux-gnu",
433 "i686-pc-linux-gnu",
434 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000435 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000436 "x86_64-unknown-linux-gnu",
437 "x86_64-unknown-linux-gnu",
438 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000439 // Gentoo x86 2009.1 stable
440 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000441 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
442 "i686-pc-linux-gnu",
443 "i686-pc-linux-gnu",
444 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000445 // Gentoo x86 2009.0 stable
446 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000447 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
448 "i686-pc-linux-gnu",
449 "i686-pc-linux-gnu",
450 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000451 // Gentoo x86 2008.0 stable
452 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000453 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
454 "i686-pc-linux-gnu",
455 "i686-pc-linux-gnu",
456 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000457 // Ubuntu 8.10
458 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000459 "i486-pc-linux-gnu",
460 "i486-pc-linux-gnu",
461 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000462 // Ubuntu 9.04
463 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000464 "i486-linux-gnu",
465 "i486-linux-gnu",
466 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000467 // Gentoo amd64 stable
468 AddGnuCPlusPlusIncludePaths(
469 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
470 "i686-pc-linux-gnu",
471 "i686-pc-linux-gnu",
472 triple);
Benjamin Kramer5d7a1882009-10-30 12:57:13 +0000473 // Exherbo (2009-10-26)
474 AddGnuCPlusPlusIncludePaths(
475 "/usr/include/c++/4.4.2",
476 "x86_64-pc-linux-gnu/32",
477 "x86_64-pc-linux-gnu",
478 triple);
479 AddGnuCPlusPlusIncludePaths(
480 "/usr/include/c++/4.4.2",
481 "i686-pc-linux-gnu",
482 "i686-pc-linux-gnu",
483 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000484 break;
485 case llvm::Triple::FreeBSD:
486 // DragonFly
487 AddPath("/usr/include/c++/4.1", System, true, false, false);
488 // FreeBSD
489 AddPath("/usr/include/c++/4.2", System, true, false, false);
490 break;
491 case llvm::Triple::Solaris:
492 // Solaris - Fall though..
493 case llvm::Triple::AuroraUX:
494 // AuroraUX
495 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
496 "i386-pc-solaris2.11",
497 "i386-pc-solaris2.11",
498 triple);
499 break;
500 default:
501 break;
502 }
503}
504
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000505void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
506 const llvm::Triple &triple) {
507 AddDefaultCIncludePaths(triple);
Daniel Dunbare1665822009-11-07 04:20:39 +0000508
509 // Add the default framework include paths on Darwin.
510 if (triple.getOS() == llvm::Triple::Darwin) {
511 AddPath("/System/Library/Frameworks", System, true, false, true);
512 AddPath("/Library/Frameworks", System, true, false, true);
513 }
514
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000515 if (Lang.CPlusPlus)
516 AddDefaultCPlusPlusIncludePaths(triple);
517}
518
Nico Weber0fca0222008-08-22 09:25:22 +0000519/// RemoveDuplicates - If there are duplicate directory entries in the specified
520/// search list, remove the later (dead) ones.
521static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
522 bool Verbose) {
523 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
524 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
525 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
526 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000527 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Chris Lattner43eee072009-02-08 01:00:10 +0000529 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Chris Lattner43eee072009-02-08 01:00:10 +0000531 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000532 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000533 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000534 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000535 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000536 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000537 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000538 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000539 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000540 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000541 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000542 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000543 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Chris Lattner30f05b52009-02-08 00:55:22 +0000546 // If we have a normal #include dir/framework/headermap that is shadowed
547 // later in the chain by a system include location, we actually want to
548 // ignore the user's request and drop the user dir... keeping the system
549 // dir. This is weird, but required to emulate GCC's search path correctly.
550 //
551 // Since dupes of system dirs are rare, just rescan to find the original
552 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000553 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000554 // Find the dir that this is the same of.
555 unsigned FirstDir;
556 for (FirstDir = 0; ; ++FirstDir) {
557 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner43eee072009-02-08 01:00:10 +0000559 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
560
Chris Lattner30f05b52009-02-08 00:55:22 +0000561 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000562 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000563 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Chris Lattner30f05b52009-02-08 00:55:22 +0000565 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000566 if (CurEntry.isNormalDir())
567 isSame = SearchEntry.getDir() == CurEntry.getDir();
568 else if (CurEntry.isFramework())
569 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000570 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000571 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
572 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Chris Lattner30f05b52009-02-08 00:55:22 +0000575 if (isSame)
576 break;
577 }
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Chris Lattner30f05b52009-02-08 00:55:22 +0000579 // If the first dir in the search path is a non-system dir, zap it
580 // instead of the system one.
581 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
582 DirToRemove = FirstDir;
583 }
584
585 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000586 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
587 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000588 if (DirToRemove != i)
589 fprintf(stderr, " as it is a non-system directory that duplicates"
590 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000591 }
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Chris Lattner7a739402008-09-26 17:46:45 +0000593 // This is reached if the current entry is a duplicate. Remove the
594 // DirToRemove (usually the current dir).
595 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000596 --i;
597 }
598}
599
600
601void InitHeaderSearch::Realize() {
602 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
603 std::vector<DirectoryLookup> SearchList;
604 SearchList = IncludeGroup[Angled];
605 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
606 IncludeGroup[System].end());
607 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
608 IncludeGroup[After].end());
609 RemoveDuplicates(SearchList, Verbose);
610 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Nico Weber0fca0222008-08-22 09:25:22 +0000612 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000613 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000614 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Nico Weber0fca0222008-08-22 09:25:22 +0000616
617 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
618 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
619 DontSearchCurDir);
620
621 // If verbose, print the list of directories that will be searched.
622 if (Verbose) {
623 fprintf(stderr, "#include \"...\" search starts here:\n");
624 unsigned QuotedIdx = IncludeGroup[Quoted].size();
625 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
626 if (i == QuotedIdx)
627 fprintf(stderr, "#include <...> search starts here:\n");
628 const char *Name = SearchList[i].getName();
629 const char *Suffix;
630 if (SearchList[i].isNormalDir())
631 Suffix = "";
632 else if (SearchList[i].isFramework())
633 Suffix = " (framework directory)";
634 else {
635 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
636 Suffix = " (headermap)";
637 }
638 fprintf(stderr, " %s%s\n", Name, Suffix);
639 }
640 fprintf(stderr, "End of search list.\n");
641 }
642}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000643
Daniel Dunbar5814e652009-11-11 21:44:21 +0000644void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
645 const HeaderSearchOptions &HSOpts,
646 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000647 const llvm::Triple &Triple) {
648 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
649
650 // Add the user defined entries.
651 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
652 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
653 Init.AddPath(E.Path, E.Group, E.IsCXXAware, E.IsUserSupplied, E.IsFramework,
654 E.IgnoreSysRoot);
655 }
656
657 // Add entries from CPATH and friends.
658 Init.AddDelimitedPaths(HSOpts.EnvIncPath.c_str());
659 Init.AddDelimitedPaths(HSOpts.LangEnvIncPath.c_str());
660
661 if (!HSOpts.BuiltinIncludePath.empty()) {
662 // Ignore the sys root, we *always* look for clang headers relative to
663 // supplied path.
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000664 Init.AddPath(HSOpts.BuiltinIncludePath, System,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000665 false, false, false, /*IgnoreSysRoot=*/ true);
666 }
667
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000668 if (HSOpts.UseStandardIncludes)
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000669 Init.AddDefaultSystemIncludePaths(Lang, Triple);
670
671 Init.Realize();
672}