blob: 70f0b2175a48dbb85267e1a0c8358ee5c4895788 [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"
Rafael Espindolaaadd7a42009-11-13 05:13:58 +000021#include "llvm/ADT/SmallVector.h"
Rafael Espindolaf0a2f512009-11-12 05:48:41 +000022#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000023#include "llvm/ADT/Triple.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000024#include "llvm/Support/raw_ostream.h"
Nico Weber0fca0222008-08-22 09:25:22 +000025#include "llvm/System/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000026#include "llvm/Config/config.h"
Chris Lattner3daed522009-03-02 22:20:04 +000027#include <cstdio>
Mike Stump620d57a2009-10-12 20:50:45 +000028#ifdef _MSC_VER
29 #define WIN32_LEAN_AND_MEAN 1
30 #include <windows.h>
31#endif
Nico Weber0fca0222008-08-22 09:25:22 +000032using namespace clang;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000033using namespace clang::frontend;
34
35namespace {
36
37/// InitHeaderSearch - This class makes it easier to set the search paths of
38/// a HeaderSearch object. InitHeaderSearch stores several search path lists
39/// internally, which can be sent to a HeaderSearch object in one swoop.
40class InitHeaderSearch {
41 std::vector<DirectoryLookup> IncludeGroup[4];
42 HeaderSearch& Headers;
43 bool Verbose;
44 std::string isysroot;
45
46public:
47
48 InitHeaderSearch(HeaderSearch &HS,
49 bool verbose = false, const std::string &iSysroot = "")
50 : Headers(HS), Verbose(verbose), isysroot(iSysroot) {}
51
52 /// AddPath - Add the specified path to the specified group list.
53 void AddPath(const llvm::StringRef &Path, IncludeDirGroup Group,
54 bool isCXXAware, bool isUserSupplied,
55 bool isFramework, bool IgnoreSysRoot = false);
56
57 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to suport a gnu
58 /// libstdc++.
Rafael Espindolaab7ae952009-11-16 19:49:37 +000059 void AddGnuCPlusPlusIncludePaths(const std::string &Base,
60 const char *ArchDir,
61 const char *Dir32,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000062 const char *Dir64,
63 const llvm::Triple &triple);
64
65 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW
66 /// libstdc++.
67 void AddMinGWCPlusPlusIncludePaths(const std::string &Base,
68 const char *Arch,
69 const char *Version);
70
71 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
72 /// separator. The processing follows that of the CPATH variable for gcc.
73 void AddDelimitedPaths(const char *String);
74
75 // AddDefaultCIncludePaths - Add paths that should always be searched.
76 void AddDefaultCIncludePaths(const llvm::Triple &triple);
77
78 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
79 // compiling c++.
80 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
81
82 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
83 /// that e.g. stdio.h is found.
84 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
85 const llvm::Triple &triple);
86
87 /// Realize - Merges all search path lists into one list and send it to
88 /// HeaderSearch.
89 void Realize();
90};
91
92}
Nico Weber0fca0222008-08-22 09:25:22 +000093
Benjamin Kramer458fb102009-09-05 09:49:39 +000094void InitHeaderSearch::AddPath(const llvm::StringRef &Path,
95 IncludeDirGroup Group, bool isCXXAware,
96 bool isUserSupplied, bool isFramework,
97 bool IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000098 assert(!Path.empty() && "can't handle empty path here");
99 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Nico Weber0fca0222008-08-22 09:25:22 +0000101 // Compute the actual path, taking into consideration -isysroot.
102 llvm::SmallString<256> MappedPath;
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Nico Weber0fca0222008-08-22 09:25:22 +0000104 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +0000105 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +0000106 // FIXME: Portability. This should be a sys::Path interface, this doesn't
107 // handle things like C:\ right, nor win32 \\network\device\blah.
108 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
109 MappedPath.append(isysroot.begin(), isysroot.end());
110 }
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Nico Weber0fca0222008-08-22 09:25:22 +0000112 MappedPath.append(Path.begin(), Path.end());
113
114 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000115 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000116 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000117 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000118 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000119 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000120 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000121 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000122
123
Nico Weber0fca0222008-08-22 09:25:22 +0000124 // If the directory exists, add it.
Benjamin Kramer458fb102009-09-05 09:49:39 +0000125 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000126 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
127 isFramework));
128 return;
129 }
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Nico Weber0fca0222008-08-22 09:25:22 +0000131 // Check to see if this is an apple-style headermap (which are not allowed to
132 // be frameworks).
133 if (!isFramework) {
Benjamin Kramer458fb102009-09-05 09:49:39 +0000134 if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000135 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
136 // It is a headermap, add it to the search path.
137 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
138 return;
139 }
140 }
141 }
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Nico Weber0fca0222008-08-22 09:25:22 +0000143 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +0000144 llvm::errs() << "ignoring nonexistent directory \""
145 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000146}
147
148
Daniel Dunbare1665822009-11-07 04:20:39 +0000149void InitHeaderSearch::AddDelimitedPaths(const char *at) {
150 if (*at == 0) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000151 return;
152
153 const char* delim = strchr(at, llvm::sys::PathSeparator);
154 while (delim != 0) {
155 if (delim-at == 0)
156 AddPath(".", Angled, false, true, false);
157 else
Benjamin Kramer458fb102009-09-05 09:49:39 +0000158 AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false);
Nico Weber0fca0222008-08-22 09:25:22 +0000159 at = delim + 1;
160 delim = strchr(at, llvm::sys::PathSeparator);
161 }
162 if (*at == 0)
163 AddPath(".", Angled, false, true, false);
164 else
165 AddPath(at, Angled, false, true, false);
166}
167
Mike Stumpec057662009-10-09 20:16:49 +0000168void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(const std::string &Base,
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000169 const char *ArchDir,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000170 const char *Dir32,
171 const char *Dir64,
172 const llvm::Triple &triple) {
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000173 // Add the common dirs
174 AddPath(Base, System, true, false, false);
175 AddPath(Base + "/backward", System, true, false, false);
176
177 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000178 llvm::Triple::ArchType arch = triple.getArch();
179 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000180 if (is64bit)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000181 AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000182 else
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000183 AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000184}
Nico Weber0fca0222008-08-22 09:25:22 +0000185
Mike Stump620d57a2009-10-12 20:50:45 +0000186void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(const std::string &Base,
187 const char *Arch,
188 const char *Version) {
Daniel Dunbar5c5758b2009-11-07 04:20:25 +0000189 std::string localBase = Base + "/" + Arch + "/" + Version + "/include";
190 AddPath(localBase, System, true, false, false);
191 AddPath(localBase + "/c++", System, true, false, false);
192 AddPath(localBase + "/c++/backward", System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000193}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000194
Mike Stump620d57a2009-10-12 20:50:45 +0000195 // FIXME: This probably should goto to some platform utils place.
196#ifdef _MSC_VER
197 // Read registry string.
198bool getSystemRegistryString(const char *keyPath, const char *valueName,
Mike Stump43d81762009-10-08 23:29:47 +0000199 char *value, size_t maxLength) {
200 HKEY hRootKey = NULL;
201 HKEY hKey = NULL;
202 const char* subKey = NULL;
203 DWORD valueType;
204 DWORD valueSize = maxLength - 1;
205 bool returnValue = false;
206 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
207 hRootKey = HKEY_CLASSES_ROOT;
208 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000209 }
Mike Stump43d81762009-10-08 23:29:47 +0000210 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
211 hRootKey = HKEY_USERS;
212 subKey = keyPath + 11;
213 }
214 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
215 hRootKey = HKEY_LOCAL_MACHINE;
216 subKey = keyPath + 19;
217 }
218 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
219 hRootKey = HKEY_CURRENT_USER;
220 subKey = keyPath + 18;
221 }
222 else
223 return(false);
224 long lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
225 if (lResult == ERROR_SUCCESS) {
Mike Stump620d57a2009-10-12 20:50:45 +0000226 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
227 (LPBYTE)value, &valueSize);
Mike Stump43d81762009-10-08 23:29:47 +0000228 if (lResult == ERROR_SUCCESS)
229 returnValue = true;
Mike Stump620d57a2009-10-12 20:50:45 +0000230 RegCloseKey(hKey);
Mike Stump43d81762009-10-08 23:29:47 +0000231 }
232 return(returnValue);
233}
Mike Stump620d57a2009-10-12 20:50:45 +0000234#else // _MSC_VER
235 // Read registry string.
236bool getSystemRegistryString(const char *, const char *, char *, size_t) {
237 return(false);
238}
239#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000240
Mike Stump43d81762009-10-08 23:29:47 +0000241 // Get Visual Studio installation directory.
242bool getVisualStudioDir(std::string &path) {
Mike Stump620d57a2009-10-12 20:50:45 +0000243 // Try the Windows registry first.
244 char vs80IDEInstallDir[256];
245 char vs90IDEInstallDir[256];
246 const char* vsIDEInstallDir = NULL;
247 bool has80 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000248 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000249 "InstallDir", vs80IDEInstallDir, sizeof(vs80IDEInstallDir) - 1);
250 bool has90 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000251 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000252 "InstallDir", vs90IDEInstallDir, sizeof(vs90IDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000253 // If we have both vc80 and vc90, pick version we were compiled with.
254 if (has80 && has90) {
255 #ifdef _MSC_VER
256 #if (_MSC_VER >= 1500) // VC90
Mike Stump620d57a2009-10-12 20:50:45 +0000257 vsIDEInstallDir = vs90IDEInstallDir;
258 #elif (_MSC_VER == 1400) // VC80
259 vsIDEInstallDir = vs80IDEInstallDir;
260 #else
261 vsIDEInstallDir = vs90IDEInstallDir;
262 #endif
263 #else
264 vsIDEInstallDir = vs90IDEInstallDir;
265 #endif
266 }
267 else if (has90)
268 vsIDEInstallDir = vs90IDEInstallDir;
269 else if (has80)
270 vsIDEInstallDir = vs80IDEInstallDir;
271 if (vsIDEInstallDir && *vsIDEInstallDir) {
272 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
273 if (p)
274 *p = '\0';
275 path = vsIDEInstallDir;
276 return(true);
277 }
278 else {
279 // Try the environment.
280 const char* vs90comntools = getenv("VS90COMNTOOLS");
281 const char* vs80comntools = getenv("VS80COMNTOOLS");
282 const char* vscomntools = NULL;
283 // If we have both vc80 and vc90, pick version we were compiled with.
284 if (vs90comntools && vs80comntools) {
285 #if (_MSC_VER >= 1500) // VC90
Mike Stump43d81762009-10-08 23:29:47 +0000286 vscomntools = vs90comntools;
287 #elif (_MSC_VER == 1400) // VC80
288 vscomntools = vs80comntools;
289 #else
290 vscomntools = vs90comntools;
291 #endif
Mike Stump620d57a2009-10-12 20:50:45 +0000292 }
293 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000294 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000295 else if (vs80comntools)
296 vscomntools = vs80comntools;
297 if (vscomntools && *vscomntools) {
298 char *p = (char*)strstr(vscomntools, "\\Common7\\Tools");
299 if (p)
300 *p = '\0';
301 path = vscomntools;
302 return(true);
303 }
304 else
305 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000306 }
Mike Stump620d57a2009-10-12 20:50:45 +0000307 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000308}
Mike Stump43d81762009-10-08 23:29:47 +0000309
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000310void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple) {
Mike Stump43d81762009-10-08 23:29:47 +0000311 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000312 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
313 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000314 llvm::SmallVector<llvm::StringRef, 5> dirs;
315 CIncludeDirs.split(dirs, ":");
316 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
317 i != dirs.end();
318 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000319 AddPath(*i, System, false, false, false);
320 return;
321 }
Mike Stump43d81762009-10-08 23:29:47 +0000322 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000323 switch (os) {
324 case llvm::Triple::Win32:
325 {
Mike Stump620d57a2009-10-12 20:50:45 +0000326 std::string VSDir;
327 if (getVisualStudioDir(VSDir)) {
328 AddPath(VSDir + "\\VC\\include", System, false, false, false);
329 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
330 System, false, false, false);
331 }
332 else {
Mike Stump43d81762009-10-08 23:29:47 +0000333 // Default install paths.
Mike Stump620d57a2009-10-12 20:50:45 +0000334 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
Mike Stump43d81762009-10-08 23:29:47 +0000335 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000336 AddPath(
337 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Mike Stump43d81762009-10-08 23:29:47 +0000338 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000339 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
340 System, false, false, false);
341 AddPath(
342 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
343 System, false, false, false);
344 // For some clang developers.
345 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
346 System, false, false, false);
347 AddPath(
348 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
349 System, false, false, false);
350 }
Mike Stump43d81762009-10-08 23:29:47 +0000351 }
352 break;
Mike Stump43d81762009-10-08 23:29:47 +0000353 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000354 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000355 AddPath("c:/mingw/include", System, true, false, false);
356 break;
357 default:
Mike Stump43d81762009-10-08 23:29:47 +0000358 break;
359 }
John Thompsond3f88342009-10-13 18:51:32 +0000360
361 AddPath("/usr/local/include", System, false, false, false);
362 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000363}
364
365void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
366 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000367 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
368 if (CxxIncludeRoot != "") {
369 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
370 if (CxxIncludeArch == "")
371 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
372 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, triple);
373 else
374 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
375 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, triple);
376 return;
377 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000378 // FIXME: temporary hack: hard-coded paths.
379 switch (os) {
380 case llvm::Triple::Cygwin:
381 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
382 System, true, false, false);
383 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
384 System, true, false, false);
385 break;
386 case llvm::Triple::MinGW64:
387 // Try gcc 4.4.0
388 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
389 // Try gcc 4.3.0
390 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
391 // Fall through.
392 case llvm::Triple::MinGW32:
393 // Try gcc 4.4.0
394 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
395 // Try gcc 4.3.0
396 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
397 break;
398 case llvm::Triple::Darwin:
399 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000400 "i686-apple-darwin10", "", "x86_64", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000401 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000402 "i686-apple-darwin8", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000403 break;
404 case llvm::Triple::Linux:
405 // Ubuntu 7.10 - Gutsy Gibbon
406 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000407 "i486-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000408 // Ubuntu 9.04
409 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000410 "x86_64-linux-gnu","32", "", triple);
Sebastian Redl5114eca2009-11-05 17:44:49 +0000411 // Ubuntu 9.10
412 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000413 "x86_64-linux-gnu", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000414 // Fedora 8
415 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000416 "i386-redhat-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000417 // Fedora 9
418 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000419 "i386-redhat-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000420 // Fedora 10
421 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000422 "i386-redhat-linux","", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000423 // openSUSE 11.1 32 bit
424 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000425 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000426 // openSUSE 11.1 64 bit
427 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000428 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000429 // openSUSE 11.2
430 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000431 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000432 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000433 "x86_64-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000434 // Arch Linux 2008-06-24
435 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000436 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000437 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000438 "x86_64-unknown-linux-gnu", "", "", 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",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000442 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000443 // Gentoo x86 2009.0 stable
444 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000445 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000446 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000447 // Gentoo x86 2008.0 stable
448 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000449 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000450 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000451 // Ubuntu 8.10
452 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000453 "i486-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000454 // Ubuntu 9.04
455 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000456 "i486-linux-gnu","", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000457 // Gentoo amd64 stable
458 AddGnuCPlusPlusIncludePaths(
459 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000460 "i686-pc-linux-gnu", "", "", triple);
Benjamin Kramer5d7a1882009-10-30 12:57:13 +0000461 // Exherbo (2009-10-26)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000462 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
463 "x86_64-pc-linux-gnu", "32", "", triple);
464 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
465 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000466 break;
467 case llvm::Triple::FreeBSD:
468 // DragonFly
469 AddPath("/usr/include/c++/4.1", System, true, false, false);
470 // FreeBSD
471 AddPath("/usr/include/c++/4.2", System, true, false, false);
472 break;
473 case llvm::Triple::Solaris:
474 // Solaris - Fall though..
475 case llvm::Triple::AuroraUX:
476 // AuroraUX
477 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000478 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000479 break;
480 default:
481 break;
482 }
483}
484
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000485void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
486 const llvm::Triple &triple) {
487 AddDefaultCIncludePaths(triple);
Daniel Dunbare1665822009-11-07 04:20:39 +0000488
489 // Add the default framework include paths on Darwin.
490 if (triple.getOS() == llvm::Triple::Darwin) {
491 AddPath("/System/Library/Frameworks", System, true, false, true);
492 AddPath("/Library/Frameworks", System, true, false, true);
493 }
494
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000495 if (Lang.CPlusPlus)
496 AddDefaultCPlusPlusIncludePaths(triple);
497}
498
Nico Weber0fca0222008-08-22 09:25:22 +0000499/// RemoveDuplicates - If there are duplicate directory entries in the specified
500/// search list, remove the later (dead) ones.
501static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
502 bool Verbose) {
503 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
504 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
505 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
506 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000507 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Chris Lattner43eee072009-02-08 01:00:10 +0000509 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Chris Lattner43eee072009-02-08 01:00:10 +0000511 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000512 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000513 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000514 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000515 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000516 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000517 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000518 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000519 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000520 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000521 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000522 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000523 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000524 }
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Chris Lattner30f05b52009-02-08 00:55:22 +0000526 // If we have a normal #include dir/framework/headermap that is shadowed
527 // later in the chain by a system include location, we actually want to
528 // ignore the user's request and drop the user dir... keeping the system
529 // dir. This is weird, but required to emulate GCC's search path correctly.
530 //
531 // Since dupes of system dirs are rare, just rescan to find the original
532 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000533 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000534 // Find the dir that this is the same of.
535 unsigned FirstDir;
536 for (FirstDir = 0; ; ++FirstDir) {
537 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattner43eee072009-02-08 01:00:10 +0000539 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
540
Chris Lattner30f05b52009-02-08 00:55:22 +0000541 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000542 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000543 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chris Lattner30f05b52009-02-08 00:55:22 +0000545 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000546 if (CurEntry.isNormalDir())
547 isSame = SearchEntry.getDir() == CurEntry.getDir();
548 else if (CurEntry.isFramework())
549 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000550 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000551 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
552 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Chris Lattner30f05b52009-02-08 00:55:22 +0000555 if (isSame)
556 break;
557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner30f05b52009-02-08 00:55:22 +0000559 // If the first dir in the search path is a non-system dir, zap it
560 // instead of the system one.
561 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
562 DirToRemove = FirstDir;
563 }
564
565 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000566 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
567 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000568 if (DirToRemove != i)
569 fprintf(stderr, " as it is a non-system directory that duplicates"
570 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000571 }
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Chris Lattner7a739402008-09-26 17:46:45 +0000573 // This is reached if the current entry is a duplicate. Remove the
574 // DirToRemove (usually the current dir).
575 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000576 --i;
577 }
578}
579
580
581void InitHeaderSearch::Realize() {
582 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
583 std::vector<DirectoryLookup> SearchList;
584 SearchList = IncludeGroup[Angled];
585 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
586 IncludeGroup[System].end());
587 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
588 IncludeGroup[After].end());
589 RemoveDuplicates(SearchList, Verbose);
590 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Nico Weber0fca0222008-08-22 09:25:22 +0000592 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000593 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000594 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Nico Weber0fca0222008-08-22 09:25:22 +0000596
597 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
598 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
599 DontSearchCurDir);
600
601 // If verbose, print the list of directories that will be searched.
602 if (Verbose) {
603 fprintf(stderr, "#include \"...\" search starts here:\n");
604 unsigned QuotedIdx = IncludeGroup[Quoted].size();
605 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
606 if (i == QuotedIdx)
607 fprintf(stderr, "#include <...> search starts here:\n");
608 const char *Name = SearchList[i].getName();
609 const char *Suffix;
610 if (SearchList[i].isNormalDir())
611 Suffix = "";
612 else if (SearchList[i].isFramework())
613 Suffix = " (framework directory)";
614 else {
615 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
616 Suffix = " (headermap)";
617 }
618 fprintf(stderr, " %s%s\n", Name, Suffix);
619 }
620 fprintf(stderr, "End of search list.\n");
621 }
622}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000623
Daniel Dunbar5814e652009-11-11 21:44:21 +0000624void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
625 const HeaderSearchOptions &HSOpts,
626 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000627 const llvm::Triple &Triple) {
628 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
629
630 // Add the user defined entries.
631 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
632 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
633 Init.AddPath(E.Path, E.Group, E.IsCXXAware, E.IsUserSupplied, E.IsFramework,
634 E.IgnoreSysRoot);
635 }
636
637 // Add entries from CPATH and friends.
638 Init.AddDelimitedPaths(HSOpts.EnvIncPath.c_str());
639 Init.AddDelimitedPaths(HSOpts.LangEnvIncPath.c_str());
640
641 if (!HSOpts.BuiltinIncludePath.empty()) {
642 // Ignore the sys root, we *always* look for clang headers relative to
643 // supplied path.
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000644 Init.AddPath(HSOpts.BuiltinIncludePath, System,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000645 false, false, false, /*IgnoreSysRoot=*/ true);
646 }
647
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000648 if (HSOpts.UseStandardIncludes)
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000649 Init.AddDefaultSystemIncludePaths(Lang, Triple);
650
651 Init.Realize();
652}