1 //===--- InitHeaderSearch.cpp - Initialize header search paths ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
29 #define WIN32_LEAN_AND_MEAN 1
32 using namespace clang
;
33 using namespace clang::frontend
;
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
;
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
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
67 void AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base
,
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
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
96 void InitHeaderSearch::AddPath(const llvm::Twine
&Path
,
97 IncludeDirGroup Group
, bool isCXXAware
,
98 bool isUserSupplied
, bool isFramework
,
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
);
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
;
123 Type
= SrcMgr::C_System
;
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
,
135 // Check to see if this is an apple-style headermap (which are not allowed to
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
));
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.
157 llvm::StringRef::size_type delim
;
158 while ((delim
= at
.find(llvm::sys::PathSeparator
)) != llvm::StringRef::npos
) {
160 AddPath(".", Angled
, false, true, false);
162 AddPath(at
.substr(0, delim
), Angled
, false, true, false);
163 at
= at
.substr(delim
+ 1);
167 AddPath(".", Angled
, false, true, false);
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
) {
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
;
184 AddPath(Base
+ "/" + ArchDir
+ "/" + Dir64
, System
, true, false, false);
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.
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
;
218 const char* subKey
= NULL
;
220 DWORD valueSize
= maxLength
- 1;
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;
241 const char *placeHolder
= strstr(subKey
, "$VERSION");
244 // If we have a $VERSION placeholder, do the highest-version search.
246 const char *keyEnd
= placeHolder
- 1;
247 const char *nextKey
= placeHolder
;
248 // Find end of previous key.
249 while ((keyEnd
> subKey
) && (*keyEnd
!= '\\'))
251 // Find end of key containing $VERSION.
252 while (*nextKey
&& (*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';
261 lResult
= RegOpenKeyEx(hRootKey
, partialKey
, 0, KEY_READ
, &hTopKey
);
262 if (lResult
== ERROR_SUCCESS
) {
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
))
274 const char *ep
= sp
+ 1;
275 while (*ep
&& (isdigit(*ep
) || (*ep
== '.')))
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
;
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
)
303 RegCloseKey(hTopKey
);
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
)
319 // Read registry string.
320 static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
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");
330 char *p
= const_cast<char *>(strstr(vcinstalldir
, "\\VC"));
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");
351 path
= vsIDEInstallDir
;
354 else if (hasVCExpressDir
&& vsExpressIDEInstallDir
[0]) {
355 char *p
= (char*)strstr(vsExpressIDEInstallDir
, "\\Common7\\IDE");
358 path
= vsExpressIDEInstallDir
;
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
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
;
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"));
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
;
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
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();
442 AddPath(*i
, System
, false, false, false);
445 llvm::Triple::OSType os
= triple
.getOS();
447 case llvm::Triple::Win32
: {
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);
455 AddPath(VSDir
+ "\\VC\\PlatformSDK\\Include",
456 System
, false, false, false);
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);
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);
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);
475 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
476 System
, false, false, false);
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);
523 case llvm::Triple::Cygwin
:
524 AddPath("/usr/include/w32api", System
, true, false, false);
526 case llvm::Triple::MinGW64
:
527 case llvm::Triple::MinGW32
:
528 AddPath("c:/mingw/include", System
, true, false, false);
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
,
548 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot
, CXX_INCLUDE_ARCH
,
549 CXX_INCLUDE_32BIT_DIR
, CXX_INCLUDE_64BIT_DIR
,
553 // FIXME: temporary hack: hard-coded paths.
555 case llvm::Triple::Cygwin
:
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");
563 case llvm::Triple::MinGW64
:
565 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.5.0");
567 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
569 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
571 case llvm::Triple::MinGW32
:
573 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
575 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
577 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
579 case llvm::Triple::Darwin
:
580 switch (triple
.getArch()) {
583 case llvm::Triple::ppc
:
584 case llvm::Triple::ppc64
:
585 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
586 "powerpc-apple-darwin10", "", "ppc64",
588 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
589 "powerpc-apple-darwin10", "", "ppc64",
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
);
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
);
610 case llvm::Triple::DragonFly
:
611 AddPath("/usr/include/c++/4.1", System
, true, false, false);
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 //===------------------------------------------------------------------===//
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
751 case llvm::Triple::FreeBSD
:
754 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple
);
756 case llvm::Triple::NetBSD
:
757 AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple
);
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++",
767 case llvm::Triple::Minix
:
768 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
771 case llvm::Triple::Solaris
:
772 // Solaris - Fall though..
773 case llvm::Triple::AuroraUX
:
775 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
776 "i386-pc-solaris2.11", "", "", triple
);
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);
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
,
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()))
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()))
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()))
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.
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())
850 if (CurEntry
.isNormalDir())
851 isSame
= SearchEntry
.getDir() == CurEntry
.getDir();
852 else if (CurEntry
.isFramework())
853 isSame
= SearchEntry
.getFrameworkDir() == CurEntry
.getFrameworkDir();
855 assert(CurEntry
.isHeaderMap() && "Not a headermap or normal dir?");
856 isSame
= SearchEntry
.getHeaderMap() == CurEntry
.getHeaderMap();
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
;
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
);
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(),
905 // If verbose, print the list of directories that will be searched.
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
) {
911 llvm::errs() << "#include <...> search starts here:\n";
912 const char *Name
= SearchList
[i
].getName();
914 if (SearchList
[i
].isNormalDir())
916 else if (SearchList
[i
].isFramework())
917 Suffix
= " (framework directory)";
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
);
948 Init
.AddDelimitedPaths(HSOpts
.ObjCEnvIncPath
);
950 Init
.AddDelimitedPaths(HSOpts
.CEnvIncPath
);
952 if (HSOpts
.UseStandardIncludes
)
953 Init
.AddDefaultSystemIncludePaths(Lang
, Triple
, HSOpts
);