blob: b630d107db5b093390cb8b7422fae3c7f50458aa [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++.
59 void AddGnuCPlusPlusIncludePaths(const std::string &Base, const char *Dir32,
60 const char *Dir64,
61 const llvm::Triple &triple);
62
63 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW
64 /// libstdc++.
65 void AddMinGWCPlusPlusIncludePaths(const std::string &Base,
66 const char *Arch,
67 const char *Version);
68
69 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
70 /// separator. The processing follows that of the CPATH variable for gcc.
71 void AddDelimitedPaths(const char *String);
72
73 // AddDefaultCIncludePaths - Add paths that should always be searched.
74 void AddDefaultCIncludePaths(const llvm::Triple &triple);
75
76 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
77 // compiling c++.
78 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
79
80 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
81 /// that e.g. stdio.h is found.
82 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
83 const llvm::Triple &triple);
84
85 /// Realize - Merges all search path lists into one list and send it to
86 /// HeaderSearch.
87 void Realize();
88};
89
90}
Nico Weber0fca0222008-08-22 09:25:22 +000091
Benjamin Kramer458fb102009-09-05 09:49:39 +000092void InitHeaderSearch::AddPath(const llvm::StringRef &Path,
93 IncludeDirGroup Group, bool isCXXAware,
94 bool isUserSupplied, bool isFramework,
95 bool IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000096 assert(!Path.empty() && "can't handle empty path here");
97 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +000098
Nico Weber0fca0222008-08-22 09:25:22 +000099 // Compute the actual path, taking into consideration -isysroot.
100 llvm::SmallString<256> MappedPath;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Nico Weber0fca0222008-08-22 09:25:22 +0000102 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +0000103 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +0000104 // FIXME: Portability. This should be a sys::Path interface, this doesn't
105 // handle things like C:\ right, nor win32 \\network\device\blah.
106 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
107 MappedPath.append(isysroot.begin(), isysroot.end());
108 }
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Nico Weber0fca0222008-08-22 09:25:22 +0000110 MappedPath.append(Path.begin(), Path.end());
111
112 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000113 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000114 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000115 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000116 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000117 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000118 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000119 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000120
121
Nico Weber0fca0222008-08-22 09:25:22 +0000122 // If the directory exists, add it.
Benjamin Kramer458fb102009-09-05 09:49:39 +0000123 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000124 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
125 isFramework));
126 return;
127 }
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Nico Weber0fca0222008-08-22 09:25:22 +0000129 // Check to see if this is an apple-style headermap (which are not allowed to
130 // be frameworks).
131 if (!isFramework) {
Benjamin Kramer458fb102009-09-05 09:49:39 +0000132 if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000133 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
134 // It is a headermap, add it to the search path.
135 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
136 return;
137 }
138 }
139 }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Nico Weber0fca0222008-08-22 09:25:22 +0000141 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +0000142 llvm::errs() << "ignoring nonexistent directory \""
143 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000144}
145
146
Daniel Dunbare1665822009-11-07 04:20:39 +0000147void InitHeaderSearch::AddDelimitedPaths(const char *at) {
148 if (*at == 0) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000149 return;
150
151 const char* delim = strchr(at, llvm::sys::PathSeparator);
152 while (delim != 0) {
153 if (delim-at == 0)
154 AddPath(".", Angled, false, true, false);
155 else
Benjamin Kramer458fb102009-09-05 09:49:39 +0000156 AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false);
Nico Weber0fca0222008-08-22 09:25:22 +0000157 at = delim + 1;
158 delim = strchr(at, llvm::sys::PathSeparator);
159 }
160 if (*at == 0)
161 AddPath(".", Angled, false, true, false);
162 else
163 AddPath(at, Angled, false, true, false);
164}
165
Mike Stumpec057662009-10-09 20:16:49 +0000166void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(const std::string &Base,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000167 const char *Dir32,
168 const char *Dir64,
169 const llvm::Triple &triple) {
170 llvm::Triple::ArchType arch = triple.getArch();
171 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
172
173 AddPath(Base, System, true, false, false);
174 if (is64bit)
175 AddPath(Base + "/" + Dir64, System, true, false, false);
176 else
177 AddPath(Base + "/" + Dir32, System, true, false, false);
178 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000179}
Nico Weber0fca0222008-08-22 09:25:22 +0000180
Mike Stump620d57a2009-10-12 20:50:45 +0000181void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(const std::string &Base,
182 const char *Arch,
183 const char *Version) {
Daniel Dunbar5c5758b2009-11-07 04:20:25 +0000184 std::string localBase = Base + "/" + Arch + "/" + Version + "/include";
185 AddPath(localBase, System, true, false, false);
186 AddPath(localBase + "/c++", System, true, false, false);
187 AddPath(localBase + "/c++/backward", System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000188}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000189
Mike Stump620d57a2009-10-12 20:50:45 +0000190 // FIXME: This probably should goto to some platform utils place.
191#ifdef _MSC_VER
192 // Read registry string.
193bool getSystemRegistryString(const char *keyPath, const char *valueName,
Mike Stump43d81762009-10-08 23:29:47 +0000194 char *value, size_t maxLength) {
195 HKEY hRootKey = NULL;
196 HKEY hKey = NULL;
197 const char* subKey = NULL;
198 DWORD valueType;
199 DWORD valueSize = maxLength - 1;
200 bool returnValue = false;
201 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
202 hRootKey = HKEY_CLASSES_ROOT;
203 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000204 }
Mike Stump43d81762009-10-08 23:29:47 +0000205 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
206 hRootKey = HKEY_USERS;
207 subKey = keyPath + 11;
208 }
209 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
210 hRootKey = HKEY_LOCAL_MACHINE;
211 subKey = keyPath + 19;
212 }
213 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
214 hRootKey = HKEY_CURRENT_USER;
215 subKey = keyPath + 18;
216 }
217 else
218 return(false);
219 long lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
220 if (lResult == ERROR_SUCCESS) {
Mike Stump620d57a2009-10-12 20:50:45 +0000221 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
222 (LPBYTE)value, &valueSize);
Mike Stump43d81762009-10-08 23:29:47 +0000223 if (lResult == ERROR_SUCCESS)
224 returnValue = true;
Mike Stump620d57a2009-10-12 20:50:45 +0000225 RegCloseKey(hKey);
Mike Stump43d81762009-10-08 23:29:47 +0000226 }
227 return(returnValue);
228}
Mike Stump620d57a2009-10-12 20:50:45 +0000229#else // _MSC_VER
230 // Read registry string.
231bool getSystemRegistryString(const char *, const char *, char *, size_t) {
232 return(false);
233}
234#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000235
Mike Stump43d81762009-10-08 23:29:47 +0000236 // Get Visual Studio installation directory.
237bool getVisualStudioDir(std::string &path) {
Mike Stump620d57a2009-10-12 20:50:45 +0000238 // Try the Windows registry first.
239 char vs80IDEInstallDir[256];
240 char vs90IDEInstallDir[256];
241 const char* vsIDEInstallDir = NULL;
242 bool has80 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000243 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000244 "InstallDir", vs80IDEInstallDir, sizeof(vs80IDEInstallDir) - 1);
245 bool has90 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000246 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000247 "InstallDir", vs90IDEInstallDir, sizeof(vs90IDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000248 // If we have both vc80 and vc90, pick version we were compiled with.
249 if (has80 && has90) {
250 #ifdef _MSC_VER
251 #if (_MSC_VER >= 1500) // VC90
Mike Stump620d57a2009-10-12 20:50:45 +0000252 vsIDEInstallDir = vs90IDEInstallDir;
253 #elif (_MSC_VER == 1400) // VC80
254 vsIDEInstallDir = vs80IDEInstallDir;
255 #else
256 vsIDEInstallDir = vs90IDEInstallDir;
257 #endif
258 #else
259 vsIDEInstallDir = vs90IDEInstallDir;
260 #endif
261 }
262 else if (has90)
263 vsIDEInstallDir = vs90IDEInstallDir;
264 else if (has80)
265 vsIDEInstallDir = vs80IDEInstallDir;
266 if (vsIDEInstallDir && *vsIDEInstallDir) {
267 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
268 if (p)
269 *p = '\0';
270 path = vsIDEInstallDir;
271 return(true);
272 }
273 else {
274 // Try the environment.
275 const char* vs90comntools = getenv("VS90COMNTOOLS");
276 const char* vs80comntools = getenv("VS80COMNTOOLS");
277 const char* vscomntools = NULL;
278 // If we have both vc80 and vc90, pick version we were compiled with.
279 if (vs90comntools && vs80comntools) {
280 #if (_MSC_VER >= 1500) // VC90
Mike Stump43d81762009-10-08 23:29:47 +0000281 vscomntools = vs90comntools;
282 #elif (_MSC_VER == 1400) // VC80
283 vscomntools = vs80comntools;
284 #else
285 vscomntools = vs90comntools;
286 #endif
Mike Stump620d57a2009-10-12 20:50:45 +0000287 }
288 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000289 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000290 else if (vs80comntools)
291 vscomntools = vs80comntools;
292 if (vscomntools && *vscomntools) {
293 char *p = (char*)strstr(vscomntools, "\\Common7\\Tools");
294 if (p)
295 *p = '\0';
296 path = vscomntools;
297 return(true);
298 }
299 else
300 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000301 }
Mike Stump620d57a2009-10-12 20:50:45 +0000302 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000303}
Mike Stump43d81762009-10-08 23:29:47 +0000304
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000305void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple) {
Mike Stump43d81762009-10-08 23:29:47 +0000306 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000307 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
308 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000309 llvm::SmallVector<llvm::StringRef, 5> dirs;
310 CIncludeDirs.split(dirs, ":");
311 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
312 i != dirs.end();
313 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000314 AddPath(*i, System, false, false, false);
315 return;
316 }
Mike Stump43d81762009-10-08 23:29:47 +0000317 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000318 switch (os) {
319 case llvm::Triple::Win32:
320 {
Mike Stump620d57a2009-10-12 20:50:45 +0000321 std::string VSDir;
322 if (getVisualStudioDir(VSDir)) {
323 AddPath(VSDir + "\\VC\\include", System, false, false, false);
324 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
325 System, false, false, false);
326 }
327 else {
Mike Stump43d81762009-10-08 23:29:47 +0000328 // Default install paths.
Mike Stump620d57a2009-10-12 20:50:45 +0000329 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
Mike Stump43d81762009-10-08 23:29:47 +0000330 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000331 AddPath(
332 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Mike Stump43d81762009-10-08 23:29:47 +0000333 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000334 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
335 System, false, false, false);
336 AddPath(
337 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
338 System, false, false, false);
339 // For some clang developers.
340 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
341 System, false, false, false);
342 AddPath(
343 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
344 System, false, false, false);
345 }
Mike Stump43d81762009-10-08 23:29:47 +0000346 }
347 break;
Mike Stump43d81762009-10-08 23:29:47 +0000348 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000349 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000350 AddPath("c:/mingw/include", System, true, false, false);
351 break;
352 default:
Mike Stump43d81762009-10-08 23:29:47 +0000353 break;
354 }
John Thompsond3f88342009-10-13 18:51:32 +0000355
356 AddPath("/usr/local/include", System, false, false, false);
357 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000358}
359
360void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
361 llvm::Triple::OSType os = triple.getOS();
362 // FIXME: temporary hack: hard-coded paths.
363 switch (os) {
364 case llvm::Triple::Cygwin:
365 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
366 System, true, false, false);
367 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
368 System, true, false, false);
369 break;
370 case llvm::Triple::MinGW64:
371 // Try gcc 4.4.0
372 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
373 // Try gcc 4.3.0
374 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
375 // Fall through.
376 case llvm::Triple::MinGW32:
377 // Try gcc 4.4.0
378 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
379 // Try gcc 4.3.0
380 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
381 break;
382 case llvm::Triple::Darwin:
383 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000384 "i686-apple-darwin10",
385 "i686-apple-darwin10/x86_64",
386 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000387 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
John Thompson40d1bb62009-11-05 22:03:02 +0000388 "i686-apple-darwin8",
389 "i686-apple-darwin8",
390 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000391 break;
392 case llvm::Triple::Linux:
393 // Ubuntu 7.10 - Gutsy Gibbon
394 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000395 "i486-linux-gnu",
396 "i486-linux-gnu",
397 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000398 // Ubuntu 9.04
399 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000400 "x86_64-linux-gnu/32",
401 "x86_64-linux-gnu",
402 triple);
Sebastian Redl5114eca2009-11-05 17:44:49 +0000403 // Ubuntu 9.10
404 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000405 "x86_64-linux-gnu/32",
406 "x86_64-linux-gnu",
407 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000408 // Fedora 8
409 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
John Thompson40d1bb62009-11-05 22:03:02 +0000410 "i386-redhat-linux",
411 "i386-redhat-linux",
412 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000413 // Fedora 9
414 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
John Thompson40d1bb62009-11-05 22:03:02 +0000415 "i386-redhat-linux",
416 "i386-redhat-linux",
417 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000418 // Fedora 10
419 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
John Thompson40d1bb62009-11-05 22:03:02 +0000420 "i386-redhat-linux",
421 "i386-redhat-linux",
422 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000423 // openSUSE 11.1 32 bit
424 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000425 "i586-suse-linux",
426 "i586-suse-linux",
427 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000428 // openSUSE 11.1 64 bit
429 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000430 "x86_64-suse-linux/32",
431 "x86_64-suse-linux",
432 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000433 // openSUSE 11.2
434 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
John Thompson40d1bb62009-11-05 22:03:02 +0000435 "i586-suse-linux",
436 "i586-suse-linux",
437 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000438 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
John Thompson40d1bb62009-11-05 22:03:02 +0000439 "x86_64-suse-linux",
440 "x86_64-suse-linux",
441 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000442 // Arch Linux 2008-06-24
443 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000444 "i686-pc-linux-gnu",
445 "i686-pc-linux-gnu",
446 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000447 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
John Thompson40d1bb62009-11-05 22:03:02 +0000448 "x86_64-unknown-linux-gnu",
449 "x86_64-unknown-linux-gnu",
450 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000451 // Gentoo x86 2009.1 stable
452 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000453 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
454 "i686-pc-linux-gnu",
455 "i686-pc-linux-gnu",
456 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000457 // Gentoo x86 2009.0 stable
458 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000459 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
460 "i686-pc-linux-gnu",
461 "i686-pc-linux-gnu",
462 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000463 // Gentoo x86 2008.0 stable
464 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000465 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
466 "i686-pc-linux-gnu",
467 "i686-pc-linux-gnu",
468 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000469 // Ubuntu 8.10
470 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000471 "i486-pc-linux-gnu",
472 "i486-pc-linux-gnu",
473 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000474 // Ubuntu 9.04
475 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
John Thompson40d1bb62009-11-05 22:03:02 +0000476 "i486-linux-gnu",
477 "i486-linux-gnu",
478 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000479 // Gentoo amd64 stable
480 AddGnuCPlusPlusIncludePaths(
481 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
482 "i686-pc-linux-gnu",
483 "i686-pc-linux-gnu",
484 triple);
Benjamin Kramer5d7a1882009-10-30 12:57:13 +0000485 // Exherbo (2009-10-26)
486 AddGnuCPlusPlusIncludePaths(
487 "/usr/include/c++/4.4.2",
488 "x86_64-pc-linux-gnu/32",
489 "x86_64-pc-linux-gnu",
490 triple);
491 AddGnuCPlusPlusIncludePaths(
492 "/usr/include/c++/4.4.2",
493 "i686-pc-linux-gnu",
494 "i686-pc-linux-gnu",
495 triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000496 break;
497 case llvm::Triple::FreeBSD:
498 // DragonFly
499 AddPath("/usr/include/c++/4.1", System, true, false, false);
500 // FreeBSD
501 AddPath("/usr/include/c++/4.2", System, true, false, false);
502 break;
503 case llvm::Triple::Solaris:
504 // Solaris - Fall though..
505 case llvm::Triple::AuroraUX:
506 // AuroraUX
507 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
508 "i386-pc-solaris2.11",
509 "i386-pc-solaris2.11",
510 triple);
511 break;
512 default:
513 break;
514 }
515}
516
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000517void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
518 const llvm::Triple &triple) {
519 AddDefaultCIncludePaths(triple);
Daniel Dunbare1665822009-11-07 04:20:39 +0000520
521 // Add the default framework include paths on Darwin.
522 if (triple.getOS() == llvm::Triple::Darwin) {
523 AddPath("/System/Library/Frameworks", System, true, false, true);
524 AddPath("/Library/Frameworks", System, true, false, true);
525 }
526
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000527 if (Lang.CPlusPlus)
528 AddDefaultCPlusPlusIncludePaths(triple);
529}
530
Nico Weber0fca0222008-08-22 09:25:22 +0000531/// RemoveDuplicates - If there are duplicate directory entries in the specified
532/// search list, remove the later (dead) ones.
533static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
534 bool Verbose) {
535 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
536 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
537 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
538 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000539 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattner43eee072009-02-08 01:00:10 +0000541 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Chris Lattner43eee072009-02-08 01:00:10 +0000543 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000544 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000545 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000546 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000547 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000548 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000549 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000550 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000551 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000552 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000553 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000554 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000555 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000556 }
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner30f05b52009-02-08 00:55:22 +0000558 // If we have a normal #include dir/framework/headermap that is shadowed
559 // later in the chain by a system include location, we actually want to
560 // ignore the user's request and drop the user dir... keeping the system
561 // dir. This is weird, but required to emulate GCC's search path correctly.
562 //
563 // Since dupes of system dirs are rare, just rescan to find the original
564 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000565 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000566 // Find the dir that this is the same of.
567 unsigned FirstDir;
568 for (FirstDir = 0; ; ++FirstDir) {
569 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattner43eee072009-02-08 01:00:10 +0000571 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
572
Chris Lattner30f05b52009-02-08 00:55:22 +0000573 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000574 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000575 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattner30f05b52009-02-08 00:55:22 +0000577 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000578 if (CurEntry.isNormalDir())
579 isSame = SearchEntry.getDir() == CurEntry.getDir();
580 else if (CurEntry.isFramework())
581 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000582 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000583 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
584 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000585 }
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Chris Lattner30f05b52009-02-08 00:55:22 +0000587 if (isSame)
588 break;
589 }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattner30f05b52009-02-08 00:55:22 +0000591 // If the first dir in the search path is a non-system dir, zap it
592 // instead of the system one.
593 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
594 DirToRemove = FirstDir;
595 }
596
597 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000598 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
599 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000600 if (DirToRemove != i)
601 fprintf(stderr, " as it is a non-system directory that duplicates"
602 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000603 }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Chris Lattner7a739402008-09-26 17:46:45 +0000605 // This is reached if the current entry is a duplicate. Remove the
606 // DirToRemove (usually the current dir).
607 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000608 --i;
609 }
610}
611
612
613void InitHeaderSearch::Realize() {
614 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
615 std::vector<DirectoryLookup> SearchList;
616 SearchList = IncludeGroup[Angled];
617 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
618 IncludeGroup[System].end());
619 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
620 IncludeGroup[After].end());
621 RemoveDuplicates(SearchList, Verbose);
622 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Nico Weber0fca0222008-08-22 09:25:22 +0000624 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000625 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000626 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Nico Weber0fca0222008-08-22 09:25:22 +0000628
629 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
630 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
631 DontSearchCurDir);
632
633 // If verbose, print the list of directories that will be searched.
634 if (Verbose) {
635 fprintf(stderr, "#include \"...\" search starts here:\n");
636 unsigned QuotedIdx = IncludeGroup[Quoted].size();
637 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
638 if (i == QuotedIdx)
639 fprintf(stderr, "#include <...> search starts here:\n");
640 const char *Name = SearchList[i].getName();
641 const char *Suffix;
642 if (SearchList[i].isNormalDir())
643 Suffix = "";
644 else if (SearchList[i].isFramework())
645 Suffix = " (framework directory)";
646 else {
647 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
648 Suffix = " (headermap)";
649 }
650 fprintf(stderr, " %s%s\n", Name, Suffix);
651 }
652 fprintf(stderr, "End of search list.\n");
653 }
654}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000655
Daniel Dunbar5814e652009-11-11 21:44:21 +0000656void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
657 const HeaderSearchOptions &HSOpts,
658 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000659 const llvm::Triple &Triple) {
660 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
661
662 // Add the user defined entries.
663 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
664 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
665 Init.AddPath(E.Path, E.Group, E.IsCXXAware, E.IsUserSupplied, E.IsFramework,
666 E.IgnoreSysRoot);
667 }
668
669 // Add entries from CPATH and friends.
670 Init.AddDelimitedPaths(HSOpts.EnvIncPath.c_str());
671 Init.AddDelimitedPaths(HSOpts.LangEnvIncPath.c_str());
672
673 if (!HSOpts.BuiltinIncludePath.empty()) {
674 // Ignore the sys root, we *always* look for clang headers relative to
675 // supplied path.
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000676 Init.AddPath(HSOpts.BuiltinIncludePath, System,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000677 false, false, false, /*IgnoreSysRoot=*/ true);
678 }
679
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000680 if (HSOpts.UseStandardIncludes)
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000681 Init.AddDefaultSystemIncludePaths(Lang, Triple);
682
683 Init.Realize();
684}