blob: 7f54b52ab553070bfa6768c9c82272b7b5e1939e [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
John Thompson75ee3bd2009-11-21 00:15:52 +0000197
Mike Stump620d57a2009-10-12 20:50:45 +0000198 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000199 // This also supports a means to look for high-versioned keys by use
200 // of a $VERSION placeholder in the key path.
201 // $VERSION in the key path is a placeholder for the version number,
202 // causing the highest value path to be searched for and used.
203 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
204 // There can be additional characters in the component. Only the numberic
205 // characters are compared.
Mike Stump620d57a2009-10-12 20:50:45 +0000206bool getSystemRegistryString(const char *keyPath, const char *valueName,
Mike Stump43d81762009-10-08 23:29:47 +0000207 char *value, size_t maxLength) {
208 HKEY hRootKey = NULL;
209 HKEY hKey = NULL;
210 const char* subKey = NULL;
211 DWORD valueType;
212 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000213 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000214 bool returnValue = false;
215 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
216 hRootKey = HKEY_CLASSES_ROOT;
217 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000218 }
Mike Stump43d81762009-10-08 23:29:47 +0000219 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
220 hRootKey = HKEY_USERS;
221 subKey = keyPath + 11;
222 }
223 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
224 hRootKey = HKEY_LOCAL_MACHINE;
225 subKey = keyPath + 19;
226 }
227 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
228 hRootKey = HKEY_CURRENT_USER;
229 subKey = keyPath + 18;
230 }
231 else
232 return(false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000233 const char *placeHolder = strstr(subKey, "$VERSION");
234 char bestName[256];
235 bestName[0] = '\0';
236 // If we have a $VERSION placeholder, do the highest-version search.
237 if (placeHolder) {
238 const char *keyEnd = placeHolder - 1;
239 const char *nextKey = placeHolder;
240 // Find end of previous key.
241 while ((keyEnd > subKey) && (*keyEnd != '\\'))
242 keyEnd--;
243 // Find end of key containing $VERSION.
244 while (*nextKey && (*nextKey != '\\'))
245 nextKey++;
246 size_t partialKeyLength = keyEnd - subKey;
247 char partialKey[256];
248 if (partialKeyLength > sizeof(partialKey))
249 partialKeyLength = sizeof(partialKey);
250 strncpy(partialKey, subKey, partialKeyLength);
251 partialKey[partialKeyLength] = '\0';
252 HKEY hTopKey = NULL;
253 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
254 if (lResult == ERROR_SUCCESS) {
255 char keyName[256];
256 int bestIndex = -1;
257 double bestValue = 0.0;
258 DWORD index, size = sizeof(keyName) - 1;
259 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
260 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
261 const char *sp = keyName;
262 while (*sp && !isdigit(*sp))
263 sp++;
264 if (!*sp)
265 continue;
266 const char *ep = sp + 1;
267 while (*ep && (isdigit(*ep) || (*ep == '.')))
268 ep++;
269 char numBuf[32];
270 strncpy(numBuf, sp, sizeof(numBuf) - 1);
271 numBuf[sizeof(numBuf) - 1] = '\0';
272 double value = strtod(numBuf, NULL);
273 if (value > bestValue) {
274 bestIndex = (int)index;
275 bestValue = value;
276 strcpy(bestName, keyName);
277 }
278 size = sizeof(keyName) - 1;
279 }
280 // If we found the highest versioned key, open the key and get the value.
281 if (bestIndex != -1) {
282 // Append rest of key.
283 strncat(bestName, nextKey, sizeof(bestName) - 1);
284 bestName[sizeof(bestName) - 1] = '\0';
285 // Open the chosen key path remainder.
286 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
287 if (lResult == ERROR_SUCCESS) {
288 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
289 (LPBYTE)value, &valueSize);
290 if (lResult == ERROR_SUCCESS)
291 returnValue = true;
292 RegCloseKey(hKey);
293 }
294 }
295 RegCloseKey(hTopKey);
296 }
297 }
298 else {
299 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
300 if (lResult == ERROR_SUCCESS) {
301 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
302 (LPBYTE)value, &valueSize);
303 if (lResult == ERROR_SUCCESS)
304 returnValue = true;
305 RegCloseKey(hKey);
306 }
Mike Stump43d81762009-10-08 23:29:47 +0000307 }
308 return(returnValue);
309}
Mike Stump620d57a2009-10-12 20:50:45 +0000310#else // _MSC_VER
311 // Read registry string.
312bool getSystemRegistryString(const char *, const char *, char *, size_t) {
313 return(false);
314}
315#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000316
Mike Stump43d81762009-10-08 23:29:47 +0000317 // Get Visual Studio installation directory.
318bool getVisualStudioDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000319 char vsIDEInstallDir[256];
Mike Stump620d57a2009-10-12 20:50:45 +0000320 // Try the Windows registry first.
John Thompson75ee3bd2009-11-21 00:15:52 +0000321 bool hasVCDir = getSystemRegistryString(
322 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
323 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000324 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000325 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000326 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
327 if (p)
328 *p = '\0';
329 path = vsIDEInstallDir;
330 return(true);
331 }
332 else {
333 // Try the environment.
334 const char* vs90comntools = getenv("VS90COMNTOOLS");
335 const char* vs80comntools = getenv("VS80COMNTOOLS");
336 const char* vscomntools = NULL;
337 // If we have both vc80 and vc90, pick version we were compiled with.
338 if (vs90comntools && vs80comntools) {
339 #if (_MSC_VER >= 1500) // VC90
Mike Stump43d81762009-10-08 23:29:47 +0000340 vscomntools = vs90comntools;
341 #elif (_MSC_VER == 1400) // VC80
342 vscomntools = vs80comntools;
343 #else
344 vscomntools = vs90comntools;
345 #endif
Mike Stump620d57a2009-10-12 20:50:45 +0000346 }
347 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000348 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000349 else if (vs80comntools)
350 vscomntools = vs80comntools;
351 if (vscomntools && *vscomntools) {
352 char *p = (char*)strstr(vscomntools, "\\Common7\\Tools");
353 if (p)
354 *p = '\0';
355 path = vscomntools;
356 return(true);
357 }
358 else
359 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000360 }
Mike Stump620d57a2009-10-12 20:50:45 +0000361 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000362}
Mike Stump43d81762009-10-08 23:29:47 +0000363
John Thompson75ee3bd2009-11-21 00:15:52 +0000364 // Get Windows SDK installation directory.
365bool getWindowsSDKDir(std::string &path) {
366 char windowsSDKInstallDir[256];
367 // Try the Windows registry.
368 bool hasSDKDir = getSystemRegistryString(
369 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
370 "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1);
371 // If we have both vc80 and vc90, pick version we were compiled with.
372 if (hasSDKDir && windowsSDKInstallDir[0]) {
373 path = windowsSDKInstallDir;
374 return(true);
375 }
376 return(false);
377}
378
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000379void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple) {
Mike Stump43d81762009-10-08 23:29:47 +0000380 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000381 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
382 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000383 llvm::SmallVector<llvm::StringRef, 5> dirs;
384 CIncludeDirs.split(dirs, ":");
385 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
386 i != dirs.end();
387 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000388 AddPath(*i, System, false, false, false);
389 return;
390 }
Mike Stump43d81762009-10-08 23:29:47 +0000391 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000392 switch (os) {
393 case llvm::Triple::Win32:
394 {
Mike Stump620d57a2009-10-12 20:50:45 +0000395 std::string VSDir;
John Thompson75ee3bd2009-11-21 00:15:52 +0000396 std::string WindowsSDKDir;
Mike Stump620d57a2009-10-12 20:50:45 +0000397 if (getVisualStudioDir(VSDir)) {
398 AddPath(VSDir + "\\VC\\include", System, false, false, false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000399 if (getWindowsSDKDir(WindowsSDKDir))
400 AddPath(WindowsSDKDir, System, false, false, false);
401 else
402 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
403 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000404 }
Mike Stump43d81762009-10-08 23:29:47 +0000405 }
406 break;
Mike Stump43d81762009-10-08 23:29:47 +0000407 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000408 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000409 AddPath("c:/mingw/include", System, true, false, false);
410 break;
411 default:
Mike Stump43d81762009-10-08 23:29:47 +0000412 break;
413 }
John Thompsond3f88342009-10-13 18:51:32 +0000414
415 AddPath("/usr/local/include", System, false, false, false);
416 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000417}
418
419void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
420 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000421 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
422 if (CxxIncludeRoot != "") {
423 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
424 if (CxxIncludeArch == "")
425 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
426 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, triple);
427 else
428 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
429 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, triple);
430 return;
431 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000432 // FIXME: temporary hack: hard-coded paths.
433 switch (os) {
434 case llvm::Triple::Cygwin:
435 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
436 System, true, false, false);
437 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
438 System, true, false, false);
439 break;
440 case llvm::Triple::MinGW64:
441 // Try gcc 4.4.0
442 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
443 // Try gcc 4.3.0
444 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
445 // Fall through.
446 case llvm::Triple::MinGW32:
447 // Try gcc 4.4.0
448 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
449 // Try gcc 4.3.0
450 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
451 break;
452 case llvm::Triple::Darwin:
453 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000454 "i686-apple-darwin10", "", "x86_64", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000455 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000456 "i686-apple-darwin8", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000457 break;
458 case llvm::Triple::Linux:
459 // Ubuntu 7.10 - Gutsy Gibbon
460 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000461 "i486-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000462 // Ubuntu 9.04
463 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000464 "x86_64-linux-gnu","32", "", triple);
Sebastian Redl5114eca2009-11-05 17:44:49 +0000465 // Ubuntu 9.10
466 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000467 "x86_64-linux-gnu", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000468 // Fedora 8
469 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000470 "i386-redhat-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000471 // Fedora 9
472 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000473 "i386-redhat-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000474 // Fedora 10
475 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000476 "i386-redhat-linux","", "", triple);
Nuno Lopes189a1482009-11-17 15:28:35 +0000477
478 // Fedora 11
479 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
480 "i586-redhat-linux","", "", triple);
481
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000482 // openSUSE 11.1 32 bit
483 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000484 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000485 // openSUSE 11.1 64 bit
486 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000487 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000488 // openSUSE 11.2
489 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000490 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000491 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000492 "x86_64-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000493 // Arch Linux 2008-06-24
494 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000495 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000496 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000497 "x86_64-unknown-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000498 // Gentoo x86 2009.1 stable
499 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000500 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000501 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000502 // Gentoo x86 2009.0 stable
503 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000504 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000505 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000506 // Gentoo x86 2008.0 stable
507 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000508 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000509 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000510 // Ubuntu 8.10
511 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000512 "i486-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000513 // Ubuntu 9.04
514 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000515 "i486-linux-gnu","", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000516 // Gentoo amd64 stable
517 AddGnuCPlusPlusIncludePaths(
518 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000519 "i686-pc-linux-gnu", "", "", triple);
Benjamin Kramer5d7a1882009-10-30 12:57:13 +0000520 // Exherbo (2009-10-26)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000521 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
522 "x86_64-pc-linux-gnu", "32", "", triple);
523 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
524 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000525 break;
526 case llvm::Triple::FreeBSD:
527 // DragonFly
528 AddPath("/usr/include/c++/4.1", System, true, false, false);
529 // FreeBSD
530 AddPath("/usr/include/c++/4.2", System, true, false, false);
531 break;
532 case llvm::Triple::Solaris:
533 // Solaris - Fall though..
534 case llvm::Triple::AuroraUX:
535 // AuroraUX
536 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000537 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000538 break;
539 default:
540 break;
541 }
542}
543
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000544void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
545 const llvm::Triple &triple) {
546 AddDefaultCIncludePaths(triple);
Daniel Dunbare1665822009-11-07 04:20:39 +0000547
548 // Add the default framework include paths on Darwin.
549 if (triple.getOS() == llvm::Triple::Darwin) {
550 AddPath("/System/Library/Frameworks", System, true, false, true);
551 AddPath("/Library/Frameworks", System, true, false, true);
552 }
553
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000554 if (Lang.CPlusPlus)
555 AddDefaultCPlusPlusIncludePaths(triple);
556}
557
Nico Weber0fca0222008-08-22 09:25:22 +0000558/// RemoveDuplicates - If there are duplicate directory entries in the specified
559/// search list, remove the later (dead) ones.
560static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
561 bool Verbose) {
562 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
563 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
564 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
565 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000566 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Chris Lattner43eee072009-02-08 01:00:10 +0000568 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattner43eee072009-02-08 01:00:10 +0000570 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000571 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000572 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000573 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000574 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000575 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000576 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000577 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000578 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000579 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000580 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000581 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000582 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattner30f05b52009-02-08 00:55:22 +0000585 // If we have a normal #include dir/framework/headermap that is shadowed
586 // later in the chain by a system include location, we actually want to
587 // ignore the user's request and drop the user dir... keeping the system
588 // dir. This is weird, but required to emulate GCC's search path correctly.
589 //
590 // Since dupes of system dirs are rare, just rescan to find the original
591 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000592 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000593 // Find the dir that this is the same of.
594 unsigned FirstDir;
595 for (FirstDir = 0; ; ++FirstDir) {
596 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Chris Lattner43eee072009-02-08 01:00:10 +0000598 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
599
Chris Lattner30f05b52009-02-08 00:55:22 +0000600 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000601 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000602 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Chris Lattner30f05b52009-02-08 00:55:22 +0000604 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000605 if (CurEntry.isNormalDir())
606 isSame = SearchEntry.getDir() == CurEntry.getDir();
607 else if (CurEntry.isFramework())
608 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000609 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000610 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
611 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000612 }
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Chris Lattner30f05b52009-02-08 00:55:22 +0000614 if (isSame)
615 break;
616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Chris Lattner30f05b52009-02-08 00:55:22 +0000618 // If the first dir in the search path is a non-system dir, zap it
619 // instead of the system one.
620 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
621 DirToRemove = FirstDir;
622 }
623
624 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000625 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
626 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000627 if (DirToRemove != i)
628 fprintf(stderr, " as it is a non-system directory that duplicates"
629 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Chris Lattner7a739402008-09-26 17:46:45 +0000632 // This is reached if the current entry is a duplicate. Remove the
633 // DirToRemove (usually the current dir).
634 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000635 --i;
636 }
637}
638
639
640void InitHeaderSearch::Realize() {
641 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
642 std::vector<DirectoryLookup> SearchList;
643 SearchList = IncludeGroup[Angled];
644 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
645 IncludeGroup[System].end());
646 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
647 IncludeGroup[After].end());
648 RemoveDuplicates(SearchList, Verbose);
649 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Nico Weber0fca0222008-08-22 09:25:22 +0000651 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000652 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000653 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Nico Weber0fca0222008-08-22 09:25:22 +0000655
656 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
657 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
658 DontSearchCurDir);
659
660 // If verbose, print the list of directories that will be searched.
661 if (Verbose) {
662 fprintf(stderr, "#include \"...\" search starts here:\n");
663 unsigned QuotedIdx = IncludeGroup[Quoted].size();
664 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
665 if (i == QuotedIdx)
666 fprintf(stderr, "#include <...> search starts here:\n");
667 const char *Name = SearchList[i].getName();
668 const char *Suffix;
669 if (SearchList[i].isNormalDir())
670 Suffix = "";
671 else if (SearchList[i].isFramework())
672 Suffix = " (framework directory)";
673 else {
674 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
675 Suffix = " (headermap)";
676 }
677 fprintf(stderr, " %s%s\n", Name, Suffix);
678 }
679 fprintf(stderr, "End of search list.\n");
680 }
681}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000682
Daniel Dunbar5814e652009-11-11 21:44:21 +0000683void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
684 const HeaderSearchOptions &HSOpts,
685 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000686 const llvm::Triple &Triple) {
687 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
688
689 // Add the user defined entries.
690 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
691 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +0000692 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
693 false);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000694 }
695
696 // Add entries from CPATH and friends.
697 Init.AddDelimitedPaths(HSOpts.EnvIncPath.c_str());
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000698 if (Lang.CPlusPlus && Lang.ObjC1)
699 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath.c_str());
700 else if (Lang.CPlusPlus)
701 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath.c_str());
702 else if (Lang.ObjC1)
703 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath.c_str());
704 else
705 Init.AddDelimitedPaths(HSOpts.CEnvIncPath.c_str());
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000706
707 if (!HSOpts.BuiltinIncludePath.empty()) {
708 // Ignore the sys root, we *always* look for clang headers relative to
709 // supplied path.
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000710 Init.AddPath(HSOpts.BuiltinIncludePath, System,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000711 false, false, false, /*IgnoreSysRoot=*/ true);
712 }
713
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000714 if (HSOpts.UseStandardIncludes)
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000715 Init.AddDefaultSystemIncludePaths(Lang, Triple);
716
717 Init.Realize();
718}