Implement -working-directory.
[clang.git] / lib / Frontend / InitHeaderSearch.cpp
blobe47381e39d3714b032cfa03b8b2029e66352d39e
1 //===--- 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.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/Utils.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/LangOptions.h"
17 #include "clang/Frontend/HeaderSearchOptions.h"
18 #include "clang/Lex/HeaderSearch.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/Triple.h"
24 #include "llvm/ADT/Twine.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/System/Path.h"
27 #include "llvm/Config/config.h"
28 #ifdef _MSC_VER
29 #define WIN32_LEAN_AND_MEAN 1
30 #include <windows.h>
31 #endif
32 using namespace clang;
33 using namespace clang::frontend;
35 namespace {
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.
40 class InitHeaderSearch {
41 std::vector<DirectoryLookup> IncludeGroup[4];
42 HeaderSearch& Headers;
43 bool Verbose;
44 std::string isysroot;
46 public:
48 InitHeaderSearch(HeaderSearch &HS,
49 bool verbose = false, const std::string &iSysroot = "")
50 : Headers(HS), Verbose(verbose), isysroot(iSysroot) {}
52 /// AddPath - Add the specified path to the specified group list.
53 void AddPath(const llvm::Twine &Path, IncludeDirGroup Group,
54 bool isCXXAware, bool isUserSupplied,
55 bool isFramework, bool IgnoreSysRoot = false);
57 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
58 /// libstdc++.
59 void AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
60 llvm::StringRef ArchDir,
61 llvm::StringRef Dir32,
62 llvm::StringRef Dir64,
63 const llvm::Triple &triple);
65 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW
66 /// libstdc++.
67 void AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
68 llvm::StringRef Arch,
69 llvm::StringRef Version);
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(llvm::StringRef String);
75 // AddDefaultCIncludePaths - Add paths that should always be searched.
76 void AddDefaultCIncludePaths(const llvm::Triple &triple,
77 const HeaderSearchOptions &HSOpts);
79 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
80 // compiling c++.
81 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
83 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
84 /// that e.g. stdio.h is found.
85 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
86 const llvm::Triple &triple,
87 const HeaderSearchOptions &HSOpts);
89 /// Realize - Merges all search path lists into one list and send it to
90 /// HeaderSearch.
91 void Realize();
96 void InitHeaderSearch::AddPath(const llvm::Twine &Path,
97 IncludeDirGroup Group, bool isCXXAware,
98 bool isUserSupplied, bool isFramework,
99 bool IgnoreSysRoot) {
100 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
101 FileManager &FM = Headers.getFileMgr();
102 const FileSystemOptions &FSOpts = Headers.getFileSystemOpts();
104 // Compute the actual path, taking into consideration -isysroot.
105 llvm::SmallString<256> MappedPathStr;
106 llvm::raw_svector_ostream MappedPath(MappedPathStr);
108 // Handle isysroot.
109 if (Group == System && !IgnoreSysRoot) {
110 // FIXME: Portability. This should be a sys::Path interface, this doesn't
111 // handle things like C:\ right, nor win32 \\network\device\blah.
112 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
113 MappedPath << isysroot;
116 Path.print(MappedPath);
118 // Compute the DirectoryLookup type.
119 SrcMgr::CharacteristicKind Type;
120 if (Group == Quoted || Group == Angled)
121 Type = SrcMgr::C_User;
122 else if (isCXXAware)
123 Type = SrcMgr::C_System;
124 else
125 Type = SrcMgr::C_ExternCSystem;
128 // If the directory exists, add it.
129 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str(), FSOpts)) {
130 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
131 isFramework));
132 return;
135 // Check to see if this is an apple-style headermap (which are not allowed to
136 // be frameworks).
137 if (!isFramework) {
138 if (const FileEntry *FE = FM.getFile(MappedPath.str(), FSOpts)) {
139 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
140 // It is a headermap, add it to the search path.
141 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
142 return;
147 if (Verbose)
148 llvm::errs() << "ignoring nonexistent directory \""
149 << MappedPath.str() << "\"\n";
153 void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) {
154 if (at.empty()) // Empty string should not add '.' path.
155 return;
157 llvm::StringRef::size_type delim;
158 while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) {
159 if (delim == 0)
160 AddPath(".", Angled, false, true, false);
161 else
162 AddPath(at.substr(0, delim), Angled, false, true, false);
163 at = at.substr(delim + 1);
166 if (at.empty())
167 AddPath(".", Angled, false, true, false);
168 else
169 AddPath(at, Angled, false, true, false);
172 void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
173 llvm::StringRef ArchDir,
174 llvm::StringRef Dir32,
175 llvm::StringRef Dir64,
176 const llvm::Triple &triple) {
177 // Add the base dir
178 AddPath(Base, System, true, false, false);
180 // Add the multilib dirs
181 llvm::Triple::ArchType arch = triple.getArch();
182 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
183 if (is64bit)
184 AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false);
185 else
186 AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false);
188 // Add the backward dir
189 AddPath(Base + "/backward", System, true, false, false);
192 void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
193 llvm::StringRef Arch,
194 llvm::StringRef Version) {
195 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
196 System, true, false, false);
197 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
198 System, true, false, false);
199 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
200 System, true, false, false);
203 // FIXME: This probably should goto to some platform utils place.
204 #ifdef _MSC_VER
206 // Read registry string.
207 // This also supports a means to look for high-versioned keys by use
208 // of a $VERSION placeholder in the key path.
209 // $VERSION in the key path is a placeholder for the version number,
210 // causing the highest value path to be searched for and used.
211 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
212 // There can be additional characters in the component. Only the numberic
213 // characters are compared.
214 static bool getSystemRegistryString(const char *keyPath, const char *valueName,
215 char *value, size_t maxLength) {
216 HKEY hRootKey = NULL;
217 HKEY hKey = NULL;
218 const char* subKey = NULL;
219 DWORD valueType;
220 DWORD valueSize = maxLength - 1;
221 long lResult;
222 bool returnValue = false;
223 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
224 hRootKey = HKEY_CLASSES_ROOT;
225 subKey = keyPath + 18;
227 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
228 hRootKey = HKEY_USERS;
229 subKey = keyPath + 11;
231 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
232 hRootKey = HKEY_LOCAL_MACHINE;
233 subKey = keyPath + 19;
235 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
236 hRootKey = HKEY_CURRENT_USER;
237 subKey = keyPath + 18;
239 else
240 return(false);
241 const char *placeHolder = strstr(subKey, "$VERSION");
242 char bestName[256];
243 bestName[0] = '\0';
244 // If we have a $VERSION placeholder, do the highest-version search.
245 if (placeHolder) {
246 const char *keyEnd = placeHolder - 1;
247 const char *nextKey = placeHolder;
248 // Find end of previous key.
249 while ((keyEnd > subKey) && (*keyEnd != '\\'))
250 keyEnd--;
251 // Find end of key containing $VERSION.
252 while (*nextKey && (*nextKey != '\\'))
253 nextKey++;
254 size_t partialKeyLength = keyEnd - subKey;
255 char partialKey[256];
256 if (partialKeyLength > sizeof(partialKey))
257 partialKeyLength = sizeof(partialKey);
258 strncpy(partialKey, subKey, partialKeyLength);
259 partialKey[partialKeyLength] = '\0';
260 HKEY hTopKey = NULL;
261 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
262 if (lResult == ERROR_SUCCESS) {
263 char keyName[256];
264 int bestIndex = -1;
265 double bestValue = 0.0;
266 DWORD index, size = sizeof(keyName) - 1;
267 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
268 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
269 const char *sp = keyName;
270 while (*sp && !isdigit(*sp))
271 sp++;
272 if (!*sp)
273 continue;
274 const char *ep = sp + 1;
275 while (*ep && (isdigit(*ep) || (*ep == '.')))
276 ep++;
277 char numBuf[32];
278 strncpy(numBuf, sp, sizeof(numBuf) - 1);
279 numBuf[sizeof(numBuf) - 1] = '\0';
280 double value = strtod(numBuf, NULL);
281 if (value > bestValue) {
282 bestIndex = (int)index;
283 bestValue = value;
284 strcpy(bestName, keyName);
286 size = sizeof(keyName) - 1;
288 // If we found the highest versioned key, open the key and get the value.
289 if (bestIndex != -1) {
290 // Append rest of key.
291 strncat(bestName, nextKey, sizeof(bestName) - 1);
292 bestName[sizeof(bestName) - 1] = '\0';
293 // Open the chosen key path remainder.
294 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
295 if (lResult == ERROR_SUCCESS) {
296 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
297 (LPBYTE)value, &valueSize);
298 if (lResult == ERROR_SUCCESS)
299 returnValue = true;
300 RegCloseKey(hKey);
303 RegCloseKey(hTopKey);
306 else {
307 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
308 if (lResult == ERROR_SUCCESS) {
309 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
310 (LPBYTE)value, &valueSize);
311 if (lResult == ERROR_SUCCESS)
312 returnValue = true;
313 RegCloseKey(hKey);
316 return(returnValue);
318 #else // _MSC_VER
319 // Read registry string.
320 static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
321 return(false);
323 #endif // _MSC_VER
325 // Get Visual Studio installation directory.
326 static bool getVisualStudioDir(std::string &path) {
327 // First check the environment variables that vsvars32.bat sets.
328 const char* vcinstalldir = getenv("VCINSTALLDIR");
329 if(vcinstalldir) {
330 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
331 if (p)
332 *p = '\0';
333 path = vcinstalldir;
334 return(true);
337 char vsIDEInstallDir[256];
338 char vsExpressIDEInstallDir[256];
339 // Then try the windows registry.
340 bool hasVCDir = getSystemRegistryString(
341 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
342 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
343 bool hasVCExpressDir = getSystemRegistryString(
344 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
345 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
346 // If we have both vc80 and vc90, pick version we were compiled with.
347 if (hasVCDir && vsIDEInstallDir[0]) {
348 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
349 if (p)
350 *p = '\0';
351 path = vsIDEInstallDir;
352 return(true);
354 else if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
355 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
356 if (p)
357 *p = '\0';
358 path = vsExpressIDEInstallDir;
359 return(true);
361 else {
362 // Try the environment.
363 const char* vs100comntools = getenv("VS100COMNTOOLS");
364 const char* vs90comntools = getenv("VS90COMNTOOLS");
365 const char* vs80comntools = getenv("VS80COMNTOOLS");
366 const char* vscomntools = NULL;
368 // Try to find the version that we were compiled with
369 if(false) {}
370 #if (_MSC_VER >= 1600) // VC100
371 else if(vs100comntools) {
372 vscomntools = vs100comntools;
374 #elif (_MSC_VER == 1500) // VC80
375 else if(vs90comntools) {
376 vscomntools = vs90comntools;
378 #elif (_MSC_VER == 1400) // VC80
379 else if(vs80comntools) {
380 vscomntools = vs80comntools;
382 #endif
383 // Otherwise find any version we can
384 else if (vs100comntools)
385 vscomntools = vs100comntools;
386 else if (vs90comntools)
387 vscomntools = vs90comntools;
388 else if (vs80comntools)
389 vscomntools = vs80comntools;
391 if (vscomntools && *vscomntools) {
392 char *p = const_cast<char *>(strstr(vscomntools, "\\Common7\\Tools"));
393 if (p)
394 *p = '\0';
395 path = vscomntools;
396 return(true);
398 else
399 return(false);
401 return(false);
404 // Get Windows SDK installation directory.
405 static bool getWindowsSDKDir(std::string &path) {
406 char windowsSDKInstallDir[256];
407 // Try the Windows registry.
408 bool hasSDKDir = getSystemRegistryString(
409 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
410 "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1);
411 // If we have both vc80 and vc90, pick version we were compiled with.
412 if (hasSDKDir && windowsSDKInstallDir[0]) {
413 path = windowsSDKInstallDir;
414 return(true);
416 return(false);
419 void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
420 const HeaderSearchOptions &HSOpts) {
421 // FIXME: temporary hack: hard-coded paths.
422 AddPath("/usr/local/include", System, true, false, false);
424 // Builtin includes use #include_next directives and should be positioned
425 // just prior C include dirs.
426 if (HSOpts.UseBuiltinIncludes) {
427 // Ignore the sys root, we *always* look for clang headers relative to
428 // supplied path.
429 llvm::sys::Path P(HSOpts.ResourceDir);
430 P.appendComponent("include");
431 AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
434 // Add dirs specified via 'configure --with-c-include-dirs'.
435 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
436 if (CIncludeDirs != "") {
437 llvm::SmallVector<llvm::StringRef, 5> dirs;
438 CIncludeDirs.split(dirs, ":");
439 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
440 i != dirs.end();
441 ++i)
442 AddPath(*i, System, false, false, false);
443 return;
445 llvm::Triple::OSType os = triple.getOS();
446 switch (os) {
447 case llvm::Triple::Win32: {
448 std::string VSDir;
449 std::string WindowsSDKDir;
450 if (getVisualStudioDir(VSDir)) {
451 AddPath(VSDir + "\\VC\\include", System, false, false, false);
452 if (getWindowsSDKDir(WindowsSDKDir))
453 AddPath(WindowsSDKDir + "\\include", System, false, false, false);
454 else
455 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
456 System, false, false, false);
457 } else {
458 // Default install paths.
459 AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
460 System, false, false, false);
461 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
462 System, false, false, false);
463 AddPath(
464 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
465 System, false, false, false);
466 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
467 System, false, false, false);
468 AddPath(
469 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
470 System, false, false, false);
471 // For some clang developers.
472 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
473 System, false, false, false);
474 AddPath(
475 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
476 System, false, false, false);
478 break;
480 case llvm::Triple::Haiku:
481 AddPath("/boot/common/include", System, true, false, false);
482 AddPath("/boot/develop/headers/os", System, true, false, false);
483 AddPath("/boot/develop/headers/os/app", System, true, false, false);
484 AddPath("/boot/develop/headers/os/arch", System, true, false, false);
485 AddPath("/boot/develop/headers/os/device", System, true, false, false);
486 AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
487 AddPath("/boot/develop/headers/os/game", System, true, false, false);
488 AddPath("/boot/develop/headers/os/interface", System, true, false, false);
489 AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
490 AddPath("/boot/develop/headers/os/locale", System, true, false, false);
491 AddPath("/boot/develop/headers/os/mail", System, true, false, false);
492 AddPath("/boot/develop/headers/os/media", System, true, false, false);
493 AddPath("/boot/develop/headers/os/midi", System, true, false, false);
494 AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
495 AddPath("/boot/develop/headers/os/net", System, true, false, false);
496 AddPath("/boot/develop/headers/os/storage", System, true, false, false);
497 AddPath("/boot/develop/headers/os/support", System, true, false, false);
498 AddPath("/boot/develop/headers/os/translation",
499 System, true, false, false);
500 AddPath("/boot/develop/headers/os/add-ons/graphics",
501 System, true, false, false);
502 AddPath("/boot/develop/headers/os/add-ons/input_server",
503 System, true, false, false);
504 AddPath("/boot/develop/headers/os/add-ons/screen_saver",
505 System, true, false, false);
506 AddPath("/boot/develop/headers/os/add-ons/tracker",
507 System, true, false, false);
508 AddPath("/boot/develop/headers/os/be_apps/Deskbar",
509 System, true, false, false);
510 AddPath("/boot/develop/headers/os/be_apps/NetPositive",
511 System, true, false, false);
512 AddPath("/boot/develop/headers/os/be_apps/Tracker",
513 System, true, false, false);
514 AddPath("/boot/develop/headers/cpp", System, true, false, false);
515 AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
516 System, true, false, false);
517 AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
518 AddPath("/boot/develop/headers/bsd", System, true, false, false);
519 AddPath("/boot/develop/headers/glibc", System, true, false, false);
520 AddPath("/boot/develop/headers/posix", System, true, false, false);
521 AddPath("/boot/develop/headers", System, true, false, false);
522 break;
523 case llvm::Triple::Cygwin:
524 AddPath("/usr/include/w32api", System, true, false, false);
525 break;
526 case llvm::Triple::MinGW64:
527 case llvm::Triple::MinGW32:
528 AddPath("c:/mingw/include", System, true, false, false);
529 break;
530 default:
531 break;
534 AddPath("/usr/include", System, false, false, false);
537 void InitHeaderSearch::
538 AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
539 llvm::Triple::OSType os = triple.getOS();
540 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
541 if (CxxIncludeRoot != "") {
542 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
543 if (CxxIncludeArch == "")
544 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
545 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
546 triple);
547 else
548 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
549 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
550 triple);
551 return;
553 // FIXME: temporary hack: hard-coded paths.
554 switch (os) {
555 case llvm::Triple::Cygwin:
556 // Cygwin-1.7
557 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
558 // g++-4 / Cygwin-1.5
559 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
560 // FIXME: Do we support g++-3.4.4?
561 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
562 break;
563 case llvm::Triple::MinGW64:
564 // Try gcc 4.5.0
565 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.5.0");
566 // Try gcc 4.4.0
567 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
568 // Try gcc 4.3.0
569 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
570 // Fall through.
571 case llvm::Triple::MinGW32:
572 // Try gcc 4.5.0
573 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
574 // Try gcc 4.4.0
575 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
576 // Try gcc 4.3.0
577 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
578 break;
579 case llvm::Triple::Darwin:
580 switch (triple.getArch()) {
581 default: break;
583 case llvm::Triple::ppc:
584 case llvm::Triple::ppc64:
585 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
586 "powerpc-apple-darwin10", "", "ppc64",
587 triple);
588 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
589 "powerpc-apple-darwin10", "", "ppc64",
590 triple);
591 break;
593 case llvm::Triple::x86:
594 case llvm::Triple::x86_64:
595 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
596 "i686-apple-darwin10", "", "x86_64", triple);
597 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
598 "i686-apple-darwin8", "", "", triple);
599 break;
601 case llvm::Triple::arm:
602 case llvm::Triple::thumb:
603 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
604 "arm-apple-darwin10", "v7", "", triple);
605 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
606 "arm-apple-darwin10", "v6", "", triple);
607 break;
609 break;
610 case llvm::Triple::DragonFly:
611 AddPath("/usr/include/c++/4.1", System, true, false, false);
612 break;
613 case llvm::Triple::Linux:
614 //===------------------------------------------------------------------===//
615 // Debian based distros.
616 // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
617 //===------------------------------------------------------------------===//
618 // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
619 // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
620 // Debian 6.0 "squeeze" -- gcc-4.4.2
621 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
622 "x86_64-linux-gnu", "32", "", triple);
623 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
624 "i486-linux-gnu", "", "64", triple);
625 // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
626 // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
627 // Debian 5.0 "lenny" -- gcc-4.3.2
628 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
629 "x86_64-linux-gnu", "32", "", triple);
630 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
631 "i486-linux-gnu", "", "64", triple);
632 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
633 "arm-linux-gnueabi", "", "", triple);
634 // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
635 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
636 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
637 "x86_64-linux-gnu", "32", "", triple);
638 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
639 "i486-linux-gnu", "", "64", triple);
640 // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
641 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
642 "x86_64-linux-gnu", "32", "", triple);
643 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
644 "i486-linux-gnu", "", "64", triple);
646 //===------------------------------------------------------------------===//
647 // Redhat based distros.
648 //===------------------------------------------------------------------===//
649 // Fedora 14
650 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
651 "x86_64-redhat-linux", "32", "", triple);
652 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
653 "i686-redhat-linux", "", "", triple);
654 // Fedora 13
655 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
656 "x86_64-redhat-linux", "32", "", triple);
657 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
658 "i686-redhat-linux","", "", triple);
659 // Fedora 12
660 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
661 "x86_64-redhat-linux", "32", "", triple);
662 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
663 "i686-redhat-linux","", "", triple);
664 // Fedora 12 (pre-FEB-2010)
665 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
666 "x86_64-redhat-linux", "32", "", triple);
667 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
668 "i686-redhat-linux","", "", triple);
669 // Fedora 11
670 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
671 "x86_64-redhat-linux", "32", "", triple);
672 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
673 "i586-redhat-linux","", "", triple);
674 // Fedora 10
675 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
676 "x86_64-redhat-linux", "32", "", triple);
677 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
678 "i386-redhat-linux","", "", triple);
679 // Fedora 9
680 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
681 "x86_64-redhat-linux", "32", "", triple);
682 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
683 "i386-redhat-linux", "", "", triple);
684 // Fedora 8
685 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
686 "x86_64-redhat-linux", "", "", triple);
687 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
688 "i386-redhat-linux", "", "", triple);
690 //===------------------------------------------------------------------===//
692 // Exherbo (2010-01-25)
693 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
694 "x86_64-pc-linux-gnu", "32", "", triple);
695 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
696 "i686-pc-linux-gnu", "", "", triple);
698 // openSUSE 11.1 32 bit
699 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
700 "i586-suse-linux", "", "", triple);
701 // openSUSE 11.1 64 bit
702 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
703 "x86_64-suse-linux", "32", "", triple);
704 // openSUSE 11.2
705 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
706 "i586-suse-linux", "", "", triple);
707 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
708 "x86_64-suse-linux", "", "", triple);
709 // Arch Linux 2008-06-24
710 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
711 "i686-pc-linux-gnu", "", "", triple);
712 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
713 "x86_64-unknown-linux-gnu", "", "", triple);
714 // Gentoo x86 2010.0 stable
715 AddGnuCPlusPlusIncludePaths(
716 "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
717 "i686-pc-linux-gnu", "", "", triple);
718 // Gentoo x86 2009.1 stable
719 AddGnuCPlusPlusIncludePaths(
720 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
721 "i686-pc-linux-gnu", "", "", triple);
722 // Gentoo x86 2009.0 stable
723 AddGnuCPlusPlusIncludePaths(
724 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
725 "i686-pc-linux-gnu", "", "", triple);
726 // Gentoo x86 2008.0 stable
727 AddGnuCPlusPlusIncludePaths(
728 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
729 "i686-pc-linux-gnu", "", "", triple);
730 // Gentoo amd64 stable
731 AddGnuCPlusPlusIncludePaths(
732 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
733 "i686-pc-linux-gnu", "", "", triple);
735 // Gentoo amd64 gcc 4.3.2
736 AddGnuCPlusPlusIncludePaths(
737 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
738 "x86_64-pc-linux-gnu", "", "", triple);
740 // Gentoo amd64 gcc 4.4.3
741 AddGnuCPlusPlusIncludePaths(
742 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
743 "x86_64-pc-linux-gnu", "32", "", triple);
745 // Gentoo amd64 llvm-gcc trunk
746 AddGnuCPlusPlusIncludePaths(
747 "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
748 "x86_64-pc-linux-gnu", "", "", triple);
750 break;
751 case llvm::Triple::FreeBSD:
752 // FreeBSD 8.0
753 // FreeBSD 7.3
754 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
755 break;
756 case llvm::Triple::NetBSD:
757 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
758 break;
759 case llvm::Triple::OpenBSD: {
760 std::string t = triple.getTriple();
761 if (t.substr(0, 6) == "x86_64")
762 t.replace(0, 6, "amd64");
763 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
764 t, "", "", triple);
765 break;
767 case llvm::Triple::Minix:
768 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
769 "", "", "", triple);
770 break;
771 case llvm::Triple::Solaris:
772 // Solaris - Fall though..
773 case llvm::Triple::AuroraUX:
774 // AuroraUX
775 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
776 "i386-pc-solaris2.11", "", "", triple);
777 break;
778 default:
779 break;
783 void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
784 const llvm::Triple &triple,
785 const HeaderSearchOptions &HSOpts) {
786 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes) {
787 if (!HSOpts.CXXSystemIncludes.empty()) {
788 for (unsigned i = 0, e = HSOpts.CXXSystemIncludes.size(); i != e; ++i)
789 AddPath(HSOpts.CXXSystemIncludes[i], System, true, false, false);
790 } else
791 AddDefaultCPlusPlusIncludePaths(triple);
794 AddDefaultCIncludePaths(triple, HSOpts);
796 // Add the default framework include paths on Darwin.
797 if (triple.getOS() == llvm::Triple::Darwin) {
798 AddPath("/System/Library/Frameworks", System, true, false, true);
799 AddPath("/Library/Frameworks", System, true, false, true);
803 /// RemoveDuplicates - If there are duplicate directory entries in the specified
804 /// search list, remove the later (dead) ones.
805 static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
806 bool Verbose) {
807 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
808 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
809 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
810 for (unsigned i = 0; i != SearchList.size(); ++i) {
811 unsigned DirToRemove = i;
813 const DirectoryLookup &CurEntry = SearchList[i];
815 if (CurEntry.isNormalDir()) {
816 // If this isn't the first time we've seen this dir, remove it.
817 if (SeenDirs.insert(CurEntry.getDir()))
818 continue;
819 } else if (CurEntry.isFramework()) {
820 // If this isn't the first time we've seen this framework dir, remove it.
821 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
822 continue;
823 } else {
824 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
825 // If this isn't the first time we've seen this headermap, remove it.
826 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
827 continue;
830 // If we have a normal #include dir/framework/headermap that is shadowed
831 // later in the chain by a system include location, we actually want to
832 // ignore the user's request and drop the user dir... keeping the system
833 // dir. This is weird, but required to emulate GCC's search path correctly.
835 // Since dupes of system dirs are rare, just rescan to find the original
836 // that we're nuking instead of using a DenseMap.
837 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
838 // Find the dir that this is the same of.
839 unsigned FirstDir;
840 for (FirstDir = 0; ; ++FirstDir) {
841 assert(FirstDir != i && "Didn't find dupe?");
843 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
845 // If these are different lookup types, then they can't be the dupe.
846 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
847 continue;
849 bool isSame;
850 if (CurEntry.isNormalDir())
851 isSame = SearchEntry.getDir() == CurEntry.getDir();
852 else if (CurEntry.isFramework())
853 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
854 else {
855 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
856 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
859 if (isSame)
860 break;
863 // If the first dir in the search path is a non-system dir, zap it
864 // instead of the system one.
865 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
866 DirToRemove = FirstDir;
869 if (Verbose) {
870 llvm::errs() << "ignoring duplicate directory \""
871 << CurEntry.getName() << "\"\n";
872 if (DirToRemove != i)
873 llvm::errs() << " as it is a non-system directory that duplicates "
874 << "a system directory\n";
877 // This is reached if the current entry is a duplicate. Remove the
878 // DirToRemove (usually the current dir).
879 SearchList.erase(SearchList.begin()+DirToRemove);
880 --i;
885 void InitHeaderSearch::Realize() {
886 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
887 std::vector<DirectoryLookup> SearchList;
888 SearchList = IncludeGroup[Angled];
889 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
890 IncludeGroup[System].end());
891 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
892 IncludeGroup[After].end());
893 RemoveDuplicates(SearchList, Verbose);
894 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
896 // Prepend QUOTED list on the search list.
897 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
898 IncludeGroup[Quoted].end());
901 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
902 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
903 DontSearchCurDir);
905 // If verbose, print the list of directories that will be searched.
906 if (Verbose) {
907 llvm::errs() << "#include \"...\" search starts here:\n";
908 unsigned QuotedIdx = IncludeGroup[Quoted].size();
909 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
910 if (i == QuotedIdx)
911 llvm::errs() << "#include <...> search starts here:\n";
912 const char *Name = SearchList[i].getName();
913 const char *Suffix;
914 if (SearchList[i].isNormalDir())
915 Suffix = "";
916 else if (SearchList[i].isFramework())
917 Suffix = " (framework directory)";
918 else {
919 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
920 Suffix = " (headermap)";
922 llvm::errs() << " " << Name << Suffix << "\n";
924 llvm::errs() << "End of search list.\n";
928 void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
929 const HeaderSearchOptions &HSOpts,
930 const LangOptions &Lang,
931 const llvm::Triple &Triple) {
932 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
934 // Add the user defined entries.
935 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
936 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
937 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
938 !E.IsSysRootRelative);
941 // Add entries from CPATH and friends.
942 Init.AddDelimitedPaths(HSOpts.EnvIncPath);
943 if (Lang.CPlusPlus && Lang.ObjC1)
944 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
945 else if (Lang.CPlusPlus)
946 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
947 else if (Lang.ObjC1)
948 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
949 else
950 Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
952 if (HSOpts.UseStandardIncludes)
953 Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
955 Init.Realize();