blob: 9327c3f2ad1d66d040f60841934607611012c76d [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"
Mike Stump620d57a2009-10-12 20:50:45 +000027#ifdef _MSC_VER
28 #define WIN32_LEAN_AND_MEAN 1
29 #include <windows.h>
30#endif
Nico Weber0fca0222008-08-22 09:25:22 +000031using namespace clang;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000032using namespace clang::frontend;
33
34namespace {
35
36/// InitHeaderSearch - This class makes it easier to set the search paths of
37/// a HeaderSearch object. InitHeaderSearch stores several search path lists
38/// internally, which can be sent to a HeaderSearch object in one swoop.
39class InitHeaderSearch {
40 std::vector<DirectoryLookup> IncludeGroup[4];
41 HeaderSearch& Headers;
42 bool Verbose;
43 std::string isysroot;
44
45public:
46
47 InitHeaderSearch(HeaderSearch &HS,
48 bool verbose = false, const std::string &iSysroot = "")
49 : Headers(HS), Verbose(verbose), isysroot(iSysroot) {}
50
51 /// AddPath - Add the specified path to the specified group list.
52 void AddPath(const llvm::StringRef &Path, IncludeDirGroup Group,
53 bool isCXXAware, bool isUserSupplied,
54 bool isFramework, bool IgnoreSysRoot = false);
55
56 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to suport a gnu
57 /// libstdc++.
Rafael Espindolaab7ae952009-11-16 19:49:37 +000058 void AddGnuCPlusPlusIncludePaths(const std::string &Base,
59 const char *ArchDir,
60 const char *Dir32,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000061 const char *Dir64,
62 const llvm::Triple &triple);
63
64 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW
65 /// libstdc++.
66 void AddMinGWCPlusPlusIncludePaths(const std::string &Base,
67 const char *Arch,
68 const char *Version);
69
70 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
71 /// separator. The processing follows that of the CPATH variable for gcc.
72 void AddDelimitedPaths(const char *String);
73
74 // AddDefaultCIncludePaths - Add paths that should always be searched.
75 void AddDefaultCIncludePaths(const llvm::Triple &triple);
76
77 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
78 // compiling c++.
79 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
80
81 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
82 /// that e.g. stdio.h is found.
83 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
84 const llvm::Triple &triple);
85
86 /// Realize - Merges all search path lists into one list and send it to
87 /// HeaderSearch.
88 void Realize();
89};
90
91}
Nico Weber0fca0222008-08-22 09:25:22 +000092
Benjamin Kramer458fb102009-09-05 09:49:39 +000093void InitHeaderSearch::AddPath(const llvm::StringRef &Path,
94 IncludeDirGroup Group, bool isCXXAware,
95 bool isUserSupplied, bool isFramework,
96 bool IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000097 assert(!Path.empty() && "can't handle empty path here");
98 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +000099
Nico Weber0fca0222008-08-22 09:25:22 +0000100 // Compute the actual path, taking into consideration -isysroot.
101 llvm::SmallString<256> MappedPath;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Nico Weber0fca0222008-08-22 09:25:22 +0000103 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +0000104 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +0000105 // FIXME: Portability. This should be a sys::Path interface, this doesn't
106 // handle things like C:\ right, nor win32 \\network\device\blah.
107 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
108 MappedPath.append(isysroot.begin(), isysroot.end());
109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Nico Weber0fca0222008-08-22 09:25:22 +0000111 MappedPath.append(Path.begin(), Path.end());
112
113 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000114 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000115 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000116 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000117 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000118 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000119 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000120 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000121
122
Nico Weber0fca0222008-08-22 09:25:22 +0000123 // If the directory exists, add it.
Benjamin Kramer458fb102009-09-05 09:49:39 +0000124 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000125 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
126 isFramework));
127 return;
128 }
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Nico Weber0fca0222008-08-22 09:25:22 +0000130 // Check to see if this is an apple-style headermap (which are not allowed to
131 // be frameworks).
132 if (!isFramework) {
Benjamin Kramer458fb102009-09-05 09:49:39 +0000133 if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000134 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
135 // It is a headermap, add it to the search path.
136 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
137 return;
138 }
139 }
140 }
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Nico Weber0fca0222008-08-22 09:25:22 +0000142 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +0000143 llvm::errs() << "ignoring nonexistent directory \""
144 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000145}
146
147
Daniel Dunbare1665822009-11-07 04:20:39 +0000148void InitHeaderSearch::AddDelimitedPaths(const char *at) {
149 if (*at == 0) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000150 return;
151
152 const char* delim = strchr(at, llvm::sys::PathSeparator);
153 while (delim != 0) {
154 if (delim-at == 0)
155 AddPath(".", Angled, false, true, false);
156 else
Benjamin Kramer458fb102009-09-05 09:49:39 +0000157 AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false);
Nico Weber0fca0222008-08-22 09:25:22 +0000158 at = delim + 1;
159 delim = strchr(at, llvm::sys::PathSeparator);
160 }
161 if (*at == 0)
162 AddPath(".", Angled, false, true, false);
163 else
164 AddPath(at, Angled, false, true, false);
165}
166
Mike Stumpec057662009-10-09 20:16:49 +0000167void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(const std::string &Base,
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000168 const char *ArchDir,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000169 const char *Dir32,
170 const char *Dir64,
171 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000172 // Add the base dir
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000173 AddPath(Base, System, true, false, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000174
175 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000176 llvm::Triple::ArchType arch = triple.getArch();
177 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000178 if (is64bit)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000179 AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000180 else
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000181 AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000182
183 // Add the backward dir
184 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000185}
Nico Weber0fca0222008-08-22 09:25:22 +0000186
Mike Stump620d57a2009-10-12 20:50:45 +0000187void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(const std::string &Base,
188 const char *Arch,
189 const char *Version) {
Daniel Dunbar5c5758b2009-11-07 04:20:25 +0000190 std::string localBase = Base + "/" + Arch + "/" + Version + "/include";
191 AddPath(localBase, System, true, false, false);
192 AddPath(localBase + "/c++", System, true, false, false);
193 AddPath(localBase + "/c++/backward", System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000194}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000195
Mike Stump620d57a2009-10-12 20:50:45 +0000196 // FIXME: This probably should goto to some platform utils place.
197#ifdef _MSC_VER
John Thompson75ee3bd2009-11-21 00:15:52 +0000198
Mike Stump620d57a2009-10-12 20:50:45 +0000199 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000200 // This also supports a means to look for high-versioned keys by use
201 // of a $VERSION placeholder in the key path.
202 // $VERSION in the key path is a placeholder for the version number,
203 // causing the highest value path to be searched for and used.
204 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
205 // There can be additional characters in the component. Only the numberic
206 // characters are compared.
Mike Stump620d57a2009-10-12 20:50:45 +0000207bool getSystemRegistryString(const char *keyPath, const char *valueName,
Mike Stump43d81762009-10-08 23:29:47 +0000208 char *value, size_t maxLength) {
209 HKEY hRootKey = NULL;
210 HKEY hKey = NULL;
211 const char* subKey = NULL;
212 DWORD valueType;
213 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000214 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000215 bool returnValue = false;
216 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
217 hRootKey = HKEY_CLASSES_ROOT;
218 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000219 }
Mike Stump43d81762009-10-08 23:29:47 +0000220 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
221 hRootKey = HKEY_USERS;
222 subKey = keyPath + 11;
223 }
224 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
225 hRootKey = HKEY_LOCAL_MACHINE;
226 subKey = keyPath + 19;
227 }
228 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
229 hRootKey = HKEY_CURRENT_USER;
230 subKey = keyPath + 18;
231 }
232 else
233 return(false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000234 const char *placeHolder = strstr(subKey, "$VERSION");
235 char bestName[256];
236 bestName[0] = '\0';
237 // If we have a $VERSION placeholder, do the highest-version search.
238 if (placeHolder) {
239 const char *keyEnd = placeHolder - 1;
240 const char *nextKey = placeHolder;
241 // Find end of previous key.
242 while ((keyEnd > subKey) && (*keyEnd != '\\'))
243 keyEnd--;
244 // Find end of key containing $VERSION.
245 while (*nextKey && (*nextKey != '\\'))
246 nextKey++;
247 size_t partialKeyLength = keyEnd - subKey;
248 char partialKey[256];
249 if (partialKeyLength > sizeof(partialKey))
250 partialKeyLength = sizeof(partialKey);
251 strncpy(partialKey, subKey, partialKeyLength);
252 partialKey[partialKeyLength] = '\0';
253 HKEY hTopKey = NULL;
254 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
255 if (lResult == ERROR_SUCCESS) {
256 char keyName[256];
257 int bestIndex = -1;
258 double bestValue = 0.0;
259 DWORD index, size = sizeof(keyName) - 1;
260 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
261 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
262 const char *sp = keyName;
263 while (*sp && !isdigit(*sp))
264 sp++;
265 if (!*sp)
266 continue;
267 const char *ep = sp + 1;
268 while (*ep && (isdigit(*ep) || (*ep == '.')))
269 ep++;
270 char numBuf[32];
271 strncpy(numBuf, sp, sizeof(numBuf) - 1);
272 numBuf[sizeof(numBuf) - 1] = '\0';
273 double value = strtod(numBuf, NULL);
274 if (value > bestValue) {
275 bestIndex = (int)index;
276 bestValue = value;
277 strcpy(bestName, keyName);
278 }
279 size = sizeof(keyName) - 1;
280 }
281 // If we found the highest versioned key, open the key and get the value.
282 if (bestIndex != -1) {
283 // Append rest of key.
284 strncat(bestName, nextKey, sizeof(bestName) - 1);
285 bestName[sizeof(bestName) - 1] = '\0';
286 // Open the chosen key path remainder.
287 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
288 if (lResult == ERROR_SUCCESS) {
289 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
290 (LPBYTE)value, &valueSize);
291 if (lResult == ERROR_SUCCESS)
292 returnValue = true;
293 RegCloseKey(hKey);
294 }
295 }
296 RegCloseKey(hTopKey);
297 }
298 }
299 else {
300 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
301 if (lResult == ERROR_SUCCESS) {
302 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
303 (LPBYTE)value, &valueSize);
304 if (lResult == ERROR_SUCCESS)
305 returnValue = true;
306 RegCloseKey(hKey);
307 }
Mike Stump43d81762009-10-08 23:29:47 +0000308 }
309 return(returnValue);
310}
Mike Stump620d57a2009-10-12 20:50:45 +0000311#else // _MSC_VER
312 // Read registry string.
313bool getSystemRegistryString(const char *, const char *, char *, size_t) {
314 return(false);
315}
316#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000317
Mike Stump43d81762009-10-08 23:29:47 +0000318 // Get Visual Studio installation directory.
319bool getVisualStudioDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000320 char vsIDEInstallDir[256];
Mike Stump620d57a2009-10-12 20:50:45 +0000321 // Try the Windows registry first.
John Thompson75ee3bd2009-11-21 00:15:52 +0000322 bool hasVCDir = getSystemRegistryString(
323 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
324 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000325 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000326 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000327 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
328 if (p)
329 *p = '\0';
330 path = vsIDEInstallDir;
331 return(true);
332 }
333 else {
334 // Try the environment.
335 const char* vs90comntools = getenv("VS90COMNTOOLS");
336 const char* vs80comntools = getenv("VS80COMNTOOLS");
337 const char* vscomntools = NULL;
338 // If we have both vc80 and vc90, pick version we were compiled with.
339 if (vs90comntools && vs80comntools) {
340 #if (_MSC_VER >= 1500) // VC90
Mike Stump43d81762009-10-08 23:29:47 +0000341 vscomntools = vs90comntools;
342 #elif (_MSC_VER == 1400) // VC80
343 vscomntools = vs80comntools;
344 #else
345 vscomntools = vs90comntools;
346 #endif
Mike Stump620d57a2009-10-12 20:50:45 +0000347 }
348 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000349 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000350 else if (vs80comntools)
351 vscomntools = vs80comntools;
352 if (vscomntools && *vscomntools) {
353 char *p = (char*)strstr(vscomntools, "\\Common7\\Tools");
354 if (p)
355 *p = '\0';
356 path = vscomntools;
357 return(true);
358 }
359 else
360 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000361 }
Mike Stump620d57a2009-10-12 20:50:45 +0000362 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000363}
Mike Stump43d81762009-10-08 23:29:47 +0000364
John Thompson75ee3bd2009-11-21 00:15:52 +0000365 // Get Windows SDK installation directory.
366bool getWindowsSDKDir(std::string &path) {
367 char windowsSDKInstallDir[256];
368 // Try the Windows registry.
369 bool hasSDKDir = getSystemRegistryString(
370 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
371 "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1);
372 // If we have both vc80 and vc90, pick version we were compiled with.
373 if (hasSDKDir && windowsSDKInstallDir[0]) {
374 path = windowsSDKInstallDir;
375 return(true);
376 }
377 return(false);
378}
379
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000380void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple) {
Mike Stump43d81762009-10-08 23:29:47 +0000381 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000382 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
383 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000384 llvm::SmallVector<llvm::StringRef, 5> dirs;
385 CIncludeDirs.split(dirs, ":");
386 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
387 i != dirs.end();
388 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000389 AddPath(*i, System, false, false, false);
390 return;
391 }
Mike Stump43d81762009-10-08 23:29:47 +0000392 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000393 switch (os) {
394 case llvm::Triple::Win32:
395 {
Mike Stump620d57a2009-10-12 20:50:45 +0000396 std::string VSDir;
John Thompson75ee3bd2009-11-21 00:15:52 +0000397 std::string WindowsSDKDir;
Mike Stump620d57a2009-10-12 20:50:45 +0000398 if (getVisualStudioDir(VSDir)) {
399 AddPath(VSDir + "\\VC\\include", System, false, false, false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000400 if (getWindowsSDKDir(WindowsSDKDir))
401 AddPath(WindowsSDKDir, System, false, false, false);
402 else
403 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
404 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000405 }
John Thompson9319f022009-11-23 17:49:27 +0000406 else {
407 // Default install paths.
408 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
409 System, false, false, false);
410 AddPath(
411 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
412 System, false, false, false);
413 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
414 System, false, false, false);
415 AddPath(
416 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
417 System, false, false, false);
418 // For some clang developers.
419 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
420 System, false, false, false);
421 AddPath(
422 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
423 System, false, false, false);
424 }
Mike Stump43d81762009-10-08 23:29:47 +0000425 }
426 break;
Mike Stump43d81762009-10-08 23:29:47 +0000427 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000428 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000429 AddPath("c:/mingw/include", System, true, false, false);
430 break;
431 default:
Mike Stump43d81762009-10-08 23:29:47 +0000432 break;
433 }
John Thompsond3f88342009-10-13 18:51:32 +0000434
435 AddPath("/usr/local/include", System, false, false, false);
436 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000437}
438
439void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
440 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000441 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
442 if (CxxIncludeRoot != "") {
443 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
444 if (CxxIncludeArch == "")
445 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
446 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, triple);
447 else
448 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
449 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, triple);
450 return;
451 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000452 // FIXME: temporary hack: hard-coded paths.
453 switch (os) {
454 case llvm::Triple::Cygwin:
455 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
456 System, true, false, false);
457 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
458 System, true, false, false);
459 break;
460 case llvm::Triple::MinGW64:
461 // Try gcc 4.4.0
462 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
463 // Try gcc 4.3.0
464 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
465 // Fall through.
466 case llvm::Triple::MinGW32:
467 // Try gcc 4.4.0
468 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
469 // Try gcc 4.3.0
470 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
471 break;
472 case llvm::Triple::Darwin:
473 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000474 "i686-apple-darwin10", "", "x86_64", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000475 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000476 "i686-apple-darwin8", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000477 break;
478 case llvm::Triple::Linux:
479 // Ubuntu 7.10 - Gutsy Gibbon
480 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000481 "i486-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000482 // Ubuntu 9.04
483 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000484 "x86_64-linux-gnu","32", "", triple);
Sebastian Redl5114eca2009-11-05 17:44:49 +0000485 // Ubuntu 9.10
486 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000487 "x86_64-linux-gnu", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000488 // Fedora 8
489 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000490 "i386-redhat-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000491 // Fedora 9
492 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000493 "i386-redhat-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000494 // Fedora 10
495 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000496 "i386-redhat-linux","", "", triple);
Nuno Lopes189a1482009-11-17 15:28:35 +0000497
498 // Fedora 11
499 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
500 "i586-redhat-linux","", "", triple);
501
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000502 // openSUSE 11.1 32 bit
503 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000504 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000505 // openSUSE 11.1 64 bit
506 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000507 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000508 // openSUSE 11.2
509 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000510 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000511 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000512 "x86_64-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000513 // Arch Linux 2008-06-24
514 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000515 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000516 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000517 "x86_64-unknown-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000518 // Gentoo x86 2009.1 stable
519 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000520 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000521 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000522 // Gentoo x86 2009.0 stable
523 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000524 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000525 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000526 // Gentoo x86 2008.0 stable
527 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000528 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000529 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000530 // Ubuntu 8.10
531 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000532 "i486-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000533 // Ubuntu 9.04
534 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000535 "i486-linux-gnu","", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000536 // Gentoo amd64 stable
537 AddGnuCPlusPlusIncludePaths(
538 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000539 "i686-pc-linux-gnu", "", "", triple);
Benjamin Kramer5d7a1882009-10-30 12:57:13 +0000540 // Exherbo (2009-10-26)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000541 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
542 "x86_64-pc-linux-gnu", "32", "", triple);
543 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
544 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000545 break;
546 case llvm::Triple::FreeBSD:
547 // DragonFly
548 AddPath("/usr/include/c++/4.1", System, true, false, false);
549 // FreeBSD
550 AddPath("/usr/include/c++/4.2", System, true, false, false);
551 break;
552 case llvm::Triple::Solaris:
553 // Solaris - Fall though..
554 case llvm::Triple::AuroraUX:
555 // AuroraUX
556 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000557 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000558 break;
559 default:
560 break;
561 }
562}
563
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000564void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
565 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000566 if (Lang.CPlusPlus)
567 AddDefaultCPlusPlusIncludePaths(triple);
568
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000569 AddDefaultCIncludePaths(triple);
Daniel Dunbare1665822009-11-07 04:20:39 +0000570
571 // Add the default framework include paths on Darwin.
572 if (triple.getOS() == llvm::Triple::Darwin) {
573 AddPath("/System/Library/Frameworks", System, true, false, true);
574 AddPath("/Library/Frameworks", System, true, false, true);
575 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000576}
577
Nico Weber0fca0222008-08-22 09:25:22 +0000578/// RemoveDuplicates - If there are duplicate directory entries in the specified
579/// search list, remove the later (dead) ones.
580static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
581 bool Verbose) {
582 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
583 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
584 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
585 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000586 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattner43eee072009-02-08 01:00:10 +0000588 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Chris Lattner43eee072009-02-08 01:00:10 +0000590 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000591 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000592 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000593 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000594 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000595 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000596 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000597 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000598 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000599 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000600 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000601 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000602 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000603 }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Chris Lattner30f05b52009-02-08 00:55:22 +0000605 // If we have a normal #include dir/framework/headermap that is shadowed
606 // later in the chain by a system include location, we actually want to
607 // ignore the user's request and drop the user dir... keeping the system
608 // dir. This is weird, but required to emulate GCC's search path correctly.
609 //
610 // Since dupes of system dirs are rare, just rescan to find the original
611 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000612 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000613 // Find the dir that this is the same of.
614 unsigned FirstDir;
615 for (FirstDir = 0; ; ++FirstDir) {
616 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Chris Lattner43eee072009-02-08 01:00:10 +0000618 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
619
Chris Lattner30f05b52009-02-08 00:55:22 +0000620 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000621 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000622 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Chris Lattner30f05b52009-02-08 00:55:22 +0000624 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000625 if (CurEntry.isNormalDir())
626 isSame = SearchEntry.getDir() == CurEntry.getDir();
627 else if (CurEntry.isFramework())
628 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000629 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000630 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
631 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Chris Lattner30f05b52009-02-08 00:55:22 +0000634 if (isSame)
635 break;
636 }
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Chris Lattner30f05b52009-02-08 00:55:22 +0000638 // If the first dir in the search path is a non-system dir, zap it
639 // instead of the system one.
640 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
641 DirToRemove = FirstDir;
642 }
643
644 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000645 llvm::errs() << "ignoring duplicate directory \""
646 << CurEntry.getName() << "\"\n";
Chris Lattner30f05b52009-02-08 00:55:22 +0000647 if (DirToRemove != i)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000648 llvm::errs() << " as it is a non-system directory that duplicates "
649 << "a system directory\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Chris Lattner7a739402008-09-26 17:46:45 +0000652 // This is reached if the current entry is a duplicate. Remove the
653 // DirToRemove (usually the current dir).
654 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000655 --i;
656 }
657}
658
659
660void InitHeaderSearch::Realize() {
661 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
662 std::vector<DirectoryLookup> SearchList;
663 SearchList = IncludeGroup[Angled];
664 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
665 IncludeGroup[System].end());
666 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
667 IncludeGroup[After].end());
668 RemoveDuplicates(SearchList, Verbose);
669 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Nico Weber0fca0222008-08-22 09:25:22 +0000671 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000672 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000673 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Nico Weber0fca0222008-08-22 09:25:22 +0000675
676 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
677 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
678 DontSearchCurDir);
679
680 // If verbose, print the list of directories that will be searched.
681 if (Verbose) {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000682 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000683 unsigned QuotedIdx = IncludeGroup[Quoted].size();
684 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
685 if (i == QuotedIdx)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000686 llvm::errs() << "#include <...> search starts here:\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000687 const char *Name = SearchList[i].getName();
688 const char *Suffix;
689 if (SearchList[i].isNormalDir())
690 Suffix = "";
691 else if (SearchList[i].isFramework())
692 Suffix = " (framework directory)";
693 else {
694 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
695 Suffix = " (headermap)";
696 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000697 llvm::errs() << " " << Name << Suffix << "\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000698 }
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000699 llvm::errs() << "End of search list.\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000700 }
701}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000702
Daniel Dunbar5814e652009-11-11 21:44:21 +0000703void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
704 const HeaderSearchOptions &HSOpts,
705 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000706 const llvm::Triple &Triple) {
707 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
708
709 // Add the user defined entries.
710 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
711 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +0000712 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
713 false);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000714 }
715
716 // Add entries from CPATH and friends.
717 Init.AddDelimitedPaths(HSOpts.EnvIncPath.c_str());
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000718 if (Lang.CPlusPlus && Lang.ObjC1)
719 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath.c_str());
720 else if (Lang.CPlusPlus)
721 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath.c_str());
722 else if (Lang.ObjC1)
723 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath.c_str());
724 else
725 Init.AddDelimitedPaths(HSOpts.CEnvIncPath.c_str());
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000726
727 if (!HSOpts.BuiltinIncludePath.empty()) {
728 // Ignore the sys root, we *always* look for clang headers relative to
729 // supplied path.
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000730 Init.AddPath(HSOpts.BuiltinIncludePath, System,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000731 false, false, false, /*IgnoreSysRoot=*/ true);
732 }
733
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000734 if (HSOpts.UseStandardIncludes)
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000735 Init.AddDefaultSystemIncludePaths(Lang, Triple);
736
737 Init.Realize();
738}