blob: 2172f61a37fc782db572ceb15d123af61f867ad0 [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
26void InitHeaderSearch::AddPath(const std::string &Path, IncludeDirGroup Group,
27 bool isCXXAware, bool isUserSupplied,
Chris Lattner6858dd32009-02-19 06:48:28 +000028 bool isFramework, bool IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000029 assert(!Path.empty() && "can't handle empty path here");
30 FileManager &FM = Headers.getFileMgr();
31
32 // Compute the actual path, taking into consideration -isysroot.
33 llvm::SmallString<256> MappedPath;
34
35 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +000036 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000037 // FIXME: Portability. This should be a sys::Path interface, this doesn't
38 // handle things like C:\ right, nor win32 \\network\device\blah.
39 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
40 MappedPath.append(isysroot.begin(), isysroot.end());
41 }
42
43 MappedPath.append(Path.begin(), Path.end());
44
45 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +000046 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +000047 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +000048 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +000049 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +000050 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +000051 else
Chris Lattner0b9e7362008-09-26 21:18:42 +000052 Type = SrcMgr::C_ExternCSystem;
Nico Weber0fca0222008-08-22 09:25:22 +000053
54
55 // If the directory exists, add it.
56 if (const DirectoryEntry *DE = FM.getDirectory(&MappedPath[0],
57 &MappedPath[0]+
58 MappedPath.size())) {
59 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
60 isFramework));
61 return;
62 }
63
64 // Check to see if this is an apple-style headermap (which are not allowed to
65 // be frameworks).
66 if (!isFramework) {
67 if (const FileEntry *FE = FM.getFile(&MappedPath[0],
68 &MappedPath[0]+MappedPath.size())) {
69 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
70 // It is a headermap, add it to the search path.
71 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
72 return;
73 }
74 }
75 }
76
77 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +000078 llvm::errs() << "ignoring nonexistent directory \""
79 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +000080}
81
82
83void InitHeaderSearch::AddEnvVarPaths(const char *Name) {
84 const char* at = getenv(Name);
Daniel Dunbarbb952552008-10-04 20:58:18 +000085 if (!at || *at == 0) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +000086 return;
87
88 const char* delim = strchr(at, llvm::sys::PathSeparator);
89 while (delim != 0) {
90 if (delim-at == 0)
91 AddPath(".", Angled, false, true, false);
92 else
93 AddPath(std::string(at, std::string::size_type(delim-at)), Angled, false,
Chris Lattner7a739402008-09-26 17:46:45 +000094 true, false);
Nico Weber0fca0222008-08-22 09:25:22 +000095 at = delim + 1;
96 delim = strchr(at, llvm::sys::PathSeparator);
97 }
98 if (*at == 0)
99 AddPath(".", Angled, false, true, false);
100 else
101 AddPath(at, Angled, false, true, false);
102}
103
104
105void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang) {
106 // FIXME: temporary hack: hard-coded paths.
107 // FIXME: get these from the target?
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000108
109#ifdef LLVM_ON_WIN32
110 if (Lang.CPlusPlus) {
111 // Mingw32 GCC version 4
Chris Lattner6f541022009-02-18 00:25:15 +0000112 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++",
113 System, true, false, false);
114 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/mingw32",
115 System, true, false, false);
116 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/backward",
117 System, true, false, false);
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000118 }
119
120 // Mingw32 GCC version 4
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000121 AddPath("C:/mingw/include", System, false, false, false);
122#else
123
Nico Weber0fca0222008-08-22 09:25:22 +0000124 if (Lang.CPlusPlus) {
125 AddPath("/usr/include/c++/4.2.1", System, true, false, false);
126 AddPath("/usr/include/c++/4.2.1/i686-apple-darwin10", System, true, false,
127 false);
128 AddPath("/usr/include/c++/4.2.1/backward", System, true, false, false);
129
130 AddPath("/usr/include/c++/4.0.0", System, true, false, false);
131 AddPath("/usr/include/c++/4.0.0/i686-apple-darwin8", System, true, false,
132 false);
133 AddPath("/usr/include/c++/4.0.0/backward", System, true, false, false);
134
135 // Ubuntu 7.10 - Gutsy Gibbon
136 AddPath("/usr/include/c++/4.1.3", System, true, false, false);
137 AddPath("/usr/include/c++/4.1.3/i486-linux-gnu", System, true, false,
138 false);
139 AddPath("/usr/include/c++/4.1.3/backward", System, true, false, false);
140
Douglas Gregor15e92322009-06-17 21:18:36 +0000141 // Ubuntu 9.04
142 AddPath("/usr/include/c++/4.3.3", System, true, false, false);
143 AddPath("/usr/include/c++/4.3.3/x86_64-linux-gnu/", System, true, false,
144 false);
145 AddPath("/usr/include/c++/4.3.3/backward", System, true, false, false);
146
Nico Weber0fca0222008-08-22 09:25:22 +0000147 // Fedora 8
148 AddPath("/usr/include/c++/4.1.2", System, true, false, false);
149 AddPath("/usr/include/c++/4.1.2/i386-redhat-linux", System, true, false,
150 false);
151 AddPath("/usr/include/c++/4.1.2/backward", System, true, false, false);
152
153 // Fedora 9
154 AddPath("/usr/include/c++/4.3.0", System, true, false, false);
155 AddPath("/usr/include/c++/4.3.0/i386-redhat-linux", System, true, false,
156 false);
157 AddPath("/usr/include/c++/4.3.0/backward", System, true, false, false);
158
Zhongxing Xu776caef2008-12-25 09:28:01 +0000159 // Fedora 10
160 AddPath("/usr/include/c++/4.3.2", System, true, false, false);
161 AddPath("/usr/include/c++/4.3.2/i386-redhat-linux", System, true, false,
162 false);
163 AddPath("/usr/include/c++/4.3.2/backward", System, true, false, false);
164
Chris Lattneref888a42009-08-07 05:28:24 +0000165 // openSUSE 11.1
166 AddPath("/usr/include/c++/4.3", System, true, false, false);
167 AddPath("/usr/include/c++/4.3/i586-suse-linux", System, true, false,
168 false);
169 AddPath("/usr/include/c++/4.3/x86_64-suse-linux", System, true, false,
170 false);
171 AddPath("/usr/include/c++/4.3/backward", System, true, false, false);
172
173 // openSUSE 11.2
174 AddPath("/usr/include/c++/4.4", System, true, false, false);
175 AddPath("/usr/include/c++/4.4/i586-suse-linux", System, true, false,
176 false);
177 AddPath("/usr/include/c++/4.4/x86_64-suse-linux", System, true, false,
178 false);
179 AddPath("/usr/include/c++/4.4/backward", System, true, false, false);
180
Nico Weber0fca0222008-08-22 09:25:22 +0000181 // Arch Linux 2008-06-24
182 AddPath("/usr/include/c++/4.3.1", System, true, false, false);
183 AddPath("/usr/include/c++/4.3.1/i686-pc-linux-gnu", System, true, false,
184 false);
185 AddPath("/usr/include/c++/4.3.1/backward", System, true, false, false);
186 AddPath("/usr/include/c++/4.3.1/x86_64-unknown-linux-gnu", System, true,
187 false, false);
Chris Lattner5654ffd2008-08-23 18:25:07 +0000188
Nuno Lopesec9fd762009-07-26 16:14:05 +0000189 // Gentoo x86 2009.0 stable
190 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4", System,
191 true, false, false);
192 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/"
193 "i686-pc-linux-gnu", System, true, false, false);
194 AddPath(" /usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/backward",
195 System, true, false, false);
196
197 // Gentoo x86 2008.0 stable
Nuno Lopesa3d783b2008-12-07 12:11:37 +0000198 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4", System,
199 true, false, false);
200 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/"
201 "i686-pc-linux-gnu", System, true, false, false);
202 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/backward",
203 System, true, false, false);
204
Eli Friedman03d6b6e2009-08-15 03:45:14 +0000205 // Ubuntu 8.10
206 AddPath("/usr/include/c++/4.3/i486-linux-gnu", System, true, false, false);
207
Sebastian Redl4d374d42009-07-01 18:59:43 +0000208 // Gentoo amd64 stable
209 AddPath("/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4", System,
210 true, false, false);
211 AddPath("/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/"
212 "i686-pc-linux-gnu", System, true, false, false);
213 AddPath("/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/backward",
214 System, true, false, false);
215
Chris Lattner5654ffd2008-08-23 18:25:07 +0000216 // DragonFly
217 AddPath("/usr/include/c++/4.1", System, true, false, false);
Chris Lattner01e4b5c2009-02-25 18:06:37 +0000218
219 // FreeBSD
220 AddPath("/usr/include/c++/4.2", System, true, false, false);
Eli Friedman868d0162009-08-01 17:10:21 +0000221
222 // AuroraUX
223 AddPath("/opt/gcc4/include/c++/4.2.4", System, true, false, false);
Eli Friedman2ef398c2009-08-01 21:46:03 +0000224 AddPath("/opt/gcc4/include/c++/4.2.4/i386-pc-solaris2.11", System, true, false, false);
Nico Weber0fca0222008-08-22 09:25:22 +0000225 }
226
227 AddPath("/usr/local/include", System, false, false, false);
228
Nico Weber0fca0222008-08-22 09:25:22 +0000229 AddPath("/usr/include", System, false, false, false);
230 AddPath("/System/Library/Frameworks", System, true, false, true);
231 AddPath("/Library/Frameworks", System, true, false, true);
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000232#endif
Nico Weber0fca0222008-08-22 09:25:22 +0000233}
234
235void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) {
236 AddEnvVarPaths("CPATH");
237 if (Lang.CPlusPlus && Lang.ObjC1)
238 AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH");
239 else if (Lang.CPlusPlus)
240 AddEnvVarPaths("CPLUS_INCLUDE_PATH");
241 else if (Lang.ObjC1)
242 AddEnvVarPaths("OBJC_INCLUDE_PATH");
243 else
244 AddEnvVarPaths("C_INCLUDE_PATH");
245}
246
247
248/// RemoveDuplicates - If there are duplicate directory entries in the specified
249/// search list, remove the later (dead) ones.
250static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
251 bool Verbose) {
252 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
253 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
254 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
255 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000256 unsigned DirToRemove = i;
257
Chris Lattner43eee072009-02-08 01:00:10 +0000258 const DirectoryLookup &CurEntry = SearchList[i];
259
260 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000261 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000262 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000263 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000264 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000265 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000266 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000267 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000268 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000269 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000270 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000271 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000272 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000273 }
274
275 // If we have a normal #include dir/framework/headermap that is shadowed
276 // later in the chain by a system include location, we actually want to
277 // ignore the user's request and drop the user dir... keeping the system
278 // dir. This is weird, but required to emulate GCC's search path correctly.
279 //
280 // Since dupes of system dirs are rare, just rescan to find the original
281 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000282 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000283 // Find the dir that this is the same of.
284 unsigned FirstDir;
285 for (FirstDir = 0; ; ++FirstDir) {
286 assert(FirstDir != i && "Didn't find dupe?");
287
Chris Lattner43eee072009-02-08 01:00:10 +0000288 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
289
Chris Lattner30f05b52009-02-08 00:55:22 +0000290 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000291 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000292 continue;
293
294 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000295 if (CurEntry.isNormalDir())
296 isSame = SearchEntry.getDir() == CurEntry.getDir();
297 else if (CurEntry.isFramework())
298 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000299 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000300 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
301 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000302 }
303
304 if (isSame)
305 break;
306 }
Nico Weber0fca0222008-08-22 09:25:22 +0000307
Chris Lattner30f05b52009-02-08 00:55:22 +0000308 // If the first dir in the search path is a non-system dir, zap it
309 // instead of the system one.
310 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
311 DirToRemove = FirstDir;
312 }
313
314 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000315 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
316 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000317 if (DirToRemove != i)
318 fprintf(stderr, " as it is a non-system directory that duplicates"
319 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000320 }
321
Chris Lattner7a739402008-09-26 17:46:45 +0000322 // This is reached if the current entry is a duplicate. Remove the
323 // DirToRemove (usually the current dir).
324 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000325 --i;
326 }
327}
328
329
330void InitHeaderSearch::Realize() {
331 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
332 std::vector<DirectoryLookup> SearchList;
333 SearchList = IncludeGroup[Angled];
334 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
335 IncludeGroup[System].end());
336 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
337 IncludeGroup[After].end());
338 RemoveDuplicates(SearchList, Verbose);
339 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
340
341 // Prepend QUOTED list on the search list.
342 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
343 IncludeGroup[Quoted].end());
344
345
346 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
347 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
348 DontSearchCurDir);
349
350 // If verbose, print the list of directories that will be searched.
351 if (Verbose) {
352 fprintf(stderr, "#include \"...\" search starts here:\n");
353 unsigned QuotedIdx = IncludeGroup[Quoted].size();
354 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
355 if (i == QuotedIdx)
356 fprintf(stderr, "#include <...> search starts here:\n");
357 const char *Name = SearchList[i].getName();
358 const char *Suffix;
359 if (SearchList[i].isNormalDir())
360 Suffix = "";
361 else if (SearchList[i].isFramework())
362 Suffix = " (framework directory)";
363 else {
364 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
365 Suffix = " (headermap)";
366 }
367 fprintf(stderr, " %s%s\n", Name, Suffix);
368 }
369 fprintf(stderr, "End of search list.\n");
370 }
371}
372