blob: b56e3a8a9853e3ebeee97168770bd511d7220e82 [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 Dunbare1bd4e62009-03-02 06:16:29 +000014#include "clang/Frontend/InitHeaderSearch.h"
Nico Weber0fca0222008-08-22 09:25:22 +000015#include "clang/Lex/HeaderSearch.h"
16#include "clang/Basic/FileManager.h"
17#include "clang/Basic/LangOptions.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000020#include "llvm/Support/raw_ostream.h"
Nico Weber0fca0222008-08-22 09:25:22 +000021#include "llvm/System/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000022#include "llvm/Config/config.h"
Chris Lattner3daed522009-03-02 22:20:04 +000023#include <cstdio>
Nico Weber0fca0222008-08-22 09:25:22 +000024using namespace clang;
25
Benjamin Kramer458fb102009-09-05 09:49:39 +000026void InitHeaderSearch::AddPath(const llvm::StringRef &Path,
27 IncludeDirGroup Group, bool isCXXAware,
28 bool isUserSupplied, bool isFramework,
29 bool IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000030 assert(!Path.empty() && "can't handle empty path here");
31 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +000032
Nico Weber0fca0222008-08-22 09:25:22 +000033 // Compute the actual path, taking into consideration -isysroot.
34 llvm::SmallString<256> MappedPath;
Mike Stump1eb44332009-09-09 15:08:12 +000035
Nico Weber0fca0222008-08-22 09:25:22 +000036 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +000037 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000038 // FIXME: Portability. This should be a sys::Path interface, this doesn't
39 // handle things like C:\ right, nor win32 \\network\device\blah.
40 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
41 MappedPath.append(isysroot.begin(), isysroot.end());
42 }
Mike Stump1eb44332009-09-09 15:08:12 +000043
Nico Weber0fca0222008-08-22 09:25:22 +000044 MappedPath.append(Path.begin(), Path.end());
45
46 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +000047 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +000048 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +000049 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +000050 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +000051 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +000052 else
Chris Lattner0b9e7362008-09-26 21:18:42 +000053 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +000054
55
Nico Weber0fca0222008-08-22 09:25:22 +000056 // If the directory exists, add it.
Benjamin Kramer458fb102009-09-05 09:49:39 +000057 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +000058 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
59 isFramework));
60 return;
61 }
Mike Stump1eb44332009-09-09 15:08:12 +000062
Nico Weber0fca0222008-08-22 09:25:22 +000063 // Check to see if this is an apple-style headermap (which are not allowed to
64 // be frameworks).
65 if (!isFramework) {
Benjamin Kramer458fb102009-09-05 09:49:39 +000066 if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +000067 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
68 // It is a headermap, add it to the search path.
69 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
70 return;
71 }
72 }
73 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Nico Weber0fca0222008-08-22 09:25:22 +000075 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +000076 llvm::errs() << "ignoring nonexistent directory \""
77 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +000078}
79
80
81void InitHeaderSearch::AddEnvVarPaths(const char *Name) {
82 const char* at = getenv(Name);
Daniel Dunbarbb952552008-10-04 20:58:18 +000083 if (!at || *at == 0) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +000084 return;
85
86 const char* delim = strchr(at, llvm::sys::PathSeparator);
87 while (delim != 0) {
88 if (delim-at == 0)
89 AddPath(".", Angled, false, true, false);
90 else
Benjamin Kramer458fb102009-09-05 09:49:39 +000091 AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false);
Nico Weber0fca0222008-08-22 09:25:22 +000092 at = delim + 1;
93 delim = strchr(at, llvm::sys::PathSeparator);
94 }
95 if (*at == 0)
96 AddPath(".", Angled, false, true, false);
97 else
98 AddPath(at, Angled, false, true, false);
99}
100
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000101void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(std::string base,
102 std::string arch) {
103 AddPath(base, System, true, false, false);
104 AddPath(base + "/" + arch, System, true, false, false);
105 AddPath(base + "/backward", System, true, false, false);
106}
Nico Weber0fca0222008-08-22 09:25:22 +0000107
108void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang) {
109 // FIXME: temporary hack: hard-coded paths.
110 // FIXME: get these from the target?
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000111
112#ifdef LLVM_ON_WIN32
113 if (Lang.CPlusPlus) {
114 // Mingw32 GCC version 4
Chris Lattner6f541022009-02-18 00:25:15 +0000115 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++",
116 System, true, false, false);
117 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/mingw32",
118 System, true, false, false);
119 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/backward",
120 System, true, false, false);
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000121 }
122
123 // Mingw32 GCC version 4
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000124 AddPath("C:/mingw/include", System, false, false, false);
125#else
126
Nico Weber0fca0222008-08-22 09:25:22 +0000127 if (Lang.CPlusPlus) {
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000128 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
129 "i686-apple-darwin10");
Nico Weber0fca0222008-08-22 09:25:22 +0000130
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000131 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", "i686-apple-darwin8");
Nico Weber0fca0222008-08-22 09:25:22 +0000132
133 // Ubuntu 7.10 - Gutsy Gibbon
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000134 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3", "i486-linux-gnu");
Nico Weber0fca0222008-08-22 09:25:22 +0000135
Douglas Gregor15e92322009-06-17 21:18:36 +0000136 // Ubuntu 9.04
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000137 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3", "x86_64-linux-gnu");
Douglas Gregor15e92322009-06-17 21:18:36 +0000138
Nico Weber0fca0222008-08-22 09:25:22 +0000139 // Fedora 8
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000140 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2", "i386-redhat-linux");
Nico Weber0fca0222008-08-22 09:25:22 +0000141
142 // Fedora 9
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000143 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0", "i386-redhat-linux");
Nico Weber0fca0222008-08-22 09:25:22 +0000144
Zhongxing Xu776caef2008-12-25 09:28:01 +0000145 // Fedora 10
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000146 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2", "i386-redhat-linux");
Zhongxing Xu776caef2008-12-25 09:28:01 +0000147
Chris Lattneref888a42009-08-07 05:28:24 +0000148 // openSUSE 11.1
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000149 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", "i586-suse-linux");
150 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", "x86_64-suse-linux");
Chris Lattneref888a42009-08-07 05:28:24 +0000151
152 // openSUSE 11.2
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000153 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", "i586-suse-linux");
154 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", "x86_64-suse-linux");
Chris Lattneref888a42009-08-07 05:28:24 +0000155
Nico Weber0fca0222008-08-22 09:25:22 +0000156 // Arch Linux 2008-06-24
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000157 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", "i686-pc-linux-gnu");
158 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
159 "x86_64-unknown-linux-gnu");
Chris Lattner5654ffd2008-08-23 18:25:07 +0000160
Nuno Lopesec9fd762009-07-26 16:14:05 +0000161 // Gentoo x86 2009.0 stable
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000162 AddGnuCPlusPlusIncludePaths(
163 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
164 "i686-pc-linux-gnu");
Nuno Lopesec9fd762009-07-26 16:14:05 +0000165
166 // Gentoo x86 2008.0 stable
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000167 AddGnuCPlusPlusIncludePaths(
168 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
169 "i686-pc-linux-gnu");
Nuno Lopesa3d783b2008-12-07 12:11:37 +0000170
Eli Friedman03d6b6e2009-08-15 03:45:14 +0000171 // Ubuntu 8.10
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000172 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", "i486-pc-linux-gnu");
Eli Friedman03d6b6e2009-08-15 03:45:14 +0000173
Sebastian Redl4d374d42009-07-01 18:59:43 +0000174 // Gentoo amd64 stable
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000175 AddGnuCPlusPlusIncludePaths(
176 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
177 "i686-pc-linux-gnu");
Sebastian Redl4d374d42009-07-01 18:59:43 +0000178
Chris Lattner5654ffd2008-08-23 18:25:07 +0000179 // DragonFly
180 AddPath("/usr/include/c++/4.1", System, true, false, false);
Chris Lattner01e4b5c2009-02-25 18:06:37 +0000181
182 // FreeBSD
183 AddPath("/usr/include/c++/4.2", System, true, false, false);
Eli Friedman868d0162009-08-01 17:10:21 +0000184
185 // AuroraUX
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000186 AddGnuCPlusPlusIncludePaths("/Opt/gcc4/include/c++/4.2.4",
187 "i386-pc-solaris2.11");
Nico Weber0fca0222008-08-22 09:25:22 +0000188 }
189
190 AddPath("/usr/local/include", System, false, false, false);
191
Nico Weber0fca0222008-08-22 09:25:22 +0000192 AddPath("/usr/include", System, false, false, false);
193 AddPath("/System/Library/Frameworks", System, true, false, true);
194 AddPath("/Library/Frameworks", System, true, false, true);
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000195#endif
Nico Weber0fca0222008-08-22 09:25:22 +0000196}
197
198void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) {
199 AddEnvVarPaths("CPATH");
200 if (Lang.CPlusPlus && Lang.ObjC1)
201 AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH");
202 else if (Lang.CPlusPlus)
203 AddEnvVarPaths("CPLUS_INCLUDE_PATH");
204 else if (Lang.ObjC1)
205 AddEnvVarPaths("OBJC_INCLUDE_PATH");
206 else
207 AddEnvVarPaths("C_INCLUDE_PATH");
208}
209
210
211/// RemoveDuplicates - If there are duplicate directory entries in the specified
212/// search list, remove the later (dead) ones.
213static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
214 bool Verbose) {
215 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
216 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
217 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
218 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000219 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Chris Lattner43eee072009-02-08 01:00:10 +0000221 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Chris Lattner43eee072009-02-08 01:00:10 +0000223 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000224 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000225 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000226 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000227 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000228 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000229 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000230 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000231 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000232 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000233 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000234 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000235 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000236 }
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattner30f05b52009-02-08 00:55:22 +0000238 // If we have a normal #include dir/framework/headermap that is shadowed
239 // later in the chain by a system include location, we actually want to
240 // ignore the user's request and drop the user dir... keeping the system
241 // dir. This is weird, but required to emulate GCC's search path correctly.
242 //
243 // Since dupes of system dirs are rare, just rescan to find the original
244 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000245 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000246 // Find the dir that this is the same of.
247 unsigned FirstDir;
248 for (FirstDir = 0; ; ++FirstDir) {
249 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Chris Lattner43eee072009-02-08 01:00:10 +0000251 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
252
Chris Lattner30f05b52009-02-08 00:55:22 +0000253 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000254 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000255 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattner30f05b52009-02-08 00:55:22 +0000257 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000258 if (CurEntry.isNormalDir())
259 isSame = SearchEntry.getDir() == CurEntry.getDir();
260 else if (CurEntry.isFramework())
261 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000262 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000263 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
264 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Chris Lattner30f05b52009-02-08 00:55:22 +0000267 if (isSame)
268 break;
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattner30f05b52009-02-08 00:55:22 +0000271 // If the first dir in the search path is a non-system dir, zap it
272 // instead of the system one.
273 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
274 DirToRemove = FirstDir;
275 }
276
277 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000278 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
279 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000280 if (DirToRemove != i)
281 fprintf(stderr, " as it is a non-system directory that duplicates"
282 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Chris Lattner7a739402008-09-26 17:46:45 +0000285 // This is reached if the current entry is a duplicate. Remove the
286 // DirToRemove (usually the current dir).
287 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000288 --i;
289 }
290}
291
292
293void InitHeaderSearch::Realize() {
294 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
295 std::vector<DirectoryLookup> SearchList;
296 SearchList = IncludeGroup[Angled];
297 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
298 IncludeGroup[System].end());
299 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
300 IncludeGroup[After].end());
301 RemoveDuplicates(SearchList, Verbose);
302 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Nico Weber0fca0222008-08-22 09:25:22 +0000304 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000305 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000306 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Nico Weber0fca0222008-08-22 09:25:22 +0000308
309 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
310 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
311 DontSearchCurDir);
312
313 // If verbose, print the list of directories that will be searched.
314 if (Verbose) {
315 fprintf(stderr, "#include \"...\" search starts here:\n");
316 unsigned QuotedIdx = IncludeGroup[Quoted].size();
317 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
318 if (i == QuotedIdx)
319 fprintf(stderr, "#include <...> search starts here:\n");
320 const char *Name = SearchList[i].getName();
321 const char *Suffix;
322 if (SearchList[i].isNormalDir())
323 Suffix = "";
324 else if (SearchList[i].isFramework())
325 Suffix = " (framework directory)";
326 else {
327 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
328 Suffix = " (headermap)";
329 }
330 fprintf(stderr, " %s%s\n", Name, Suffix);
331 }
332 fprintf(stderr, "End of search list.\n");
333 }
334}