blob: 4c250121151516cf2427485e58f4fe9515ae9b94 [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
14#include "clang/Driver/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"
20#include "llvm/System/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000021#include "llvm/Config/config.h"
Nico Weber0fca0222008-08-22 09:25:22 +000022#include <vector>
23
24using namespace clang;
25
26void InitHeaderSearch::AddPath(const std::string &Path, IncludeDirGroup Group,
27 bool isCXXAware, bool isUserSupplied,
28 bool isFramework) {
29 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.
36 if (Group == System) {
37 // 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)
78 fprintf(stderr, "ignoring nonexistent directory \"%s\"\n", Path.c_str());
79}
80
81
82void InitHeaderSearch::AddEnvVarPaths(const char *Name) {
83 const char* at = getenv(Name);
Daniel Dunbarbb952552008-10-04 20:58:18 +000084 if (!at || *at == 0) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +000085 return;
86
87 const char* delim = strchr(at, llvm::sys::PathSeparator);
88 while (delim != 0) {
89 if (delim-at == 0)
90 AddPath(".", Angled, false, true, false);
91 else
92 AddPath(std::string(at, std::string::size_type(delim-at)), Angled, false,
Chris Lattner7a739402008-09-26 17:46:45 +000093 true, false);
Nico Weber0fca0222008-08-22 09:25:22 +000094 at = delim + 1;
95 delim = strchr(at, llvm::sys::PathSeparator);
96 }
97 if (*at == 0)
98 AddPath(".", Angled, false, true, false);
99 else
100 AddPath(at, Angled, false, true, false);
101}
102
103
104void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang) {
105 // FIXME: temporary hack: hard-coded paths.
106 // FIXME: get these from the target?
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000107
108#ifdef LLVM_ON_WIN32
109 if (Lang.CPlusPlus) {
110 // Mingw32 GCC version 4
111 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++", System, true, false, false);
112 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/mingw32", System, true, false, false);
113 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/backward", System, true, false, false);
114 }
115
116 // Mingw32 GCC version 4
117 AddPath("C:/mingw/lib/gcc/mingw32/4.3.0/include", System, false, false, false);
118 AddPath("C:/mingw/include", System, false, false, false);
119#else
120
Nico Weber0fca0222008-08-22 09:25:22 +0000121 if (Lang.CPlusPlus) {
122 AddPath("/usr/include/c++/4.2.1", System, true, false, false);
123 AddPath("/usr/include/c++/4.2.1/i686-apple-darwin10", System, true, false,
124 false);
125 AddPath("/usr/include/c++/4.2.1/backward", System, true, false, false);
126
127 AddPath("/usr/include/c++/4.0.0", System, true, false, false);
128 AddPath("/usr/include/c++/4.0.0/i686-apple-darwin8", System, true, false,
129 false);
130 AddPath("/usr/include/c++/4.0.0/backward", System, true, false, false);
131
132 // Ubuntu 7.10 - Gutsy Gibbon
133 AddPath("/usr/include/c++/4.1.3", System, true, false, false);
134 AddPath("/usr/include/c++/4.1.3/i486-linux-gnu", System, true, false,
135 false);
136 AddPath("/usr/include/c++/4.1.3/backward", System, true, false, false);
137
138 // Fedora 8
139 AddPath("/usr/include/c++/4.1.2", System, true, false, false);
140 AddPath("/usr/include/c++/4.1.2/i386-redhat-linux", System, true, false,
141 false);
142 AddPath("/usr/include/c++/4.1.2/backward", System, true, false, false);
143
144 // Fedora 9
145 AddPath("/usr/include/c++/4.3.0", System, true, false, false);
146 AddPath("/usr/include/c++/4.3.0/i386-redhat-linux", System, true, false,
147 false);
148 AddPath("/usr/include/c++/4.3.0/backward", System, true, false, false);
149
Zhongxing Xu776caef2008-12-25 09:28:01 +0000150 // Fedora 10
151 AddPath("/usr/include/c++/4.3.2", System, true, false, false);
152 AddPath("/usr/include/c++/4.3.2/i386-redhat-linux", System, true, false,
153 false);
154 AddPath("/usr/include/c++/4.3.2/backward", System, true, false, false);
155
Nico Weber0fca0222008-08-22 09:25:22 +0000156 // Arch Linux 2008-06-24
157 AddPath("/usr/include/c++/4.3.1", System, true, false, false);
158 AddPath("/usr/include/c++/4.3.1/i686-pc-linux-gnu", System, true, false,
159 false);
160 AddPath("/usr/include/c++/4.3.1/backward", System, true, false, false);
161 AddPath("/usr/include/c++/4.3.1/x86_64-unknown-linux-gnu", System, true,
162 false, false);
Chris Lattner5654ffd2008-08-23 18:25:07 +0000163
Nuno Lopesa3d783b2008-12-07 12:11:37 +0000164 // Gentoo x86 stable
165 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4", System,
166 true, false, false);
167 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/"
168 "i686-pc-linux-gnu", System, true, false, false);
169 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/backward",
170 System, true, false, false);
171
172
Chris Lattner5654ffd2008-08-23 18:25:07 +0000173 // DragonFly
174 AddPath("/usr/include/c++/4.1", System, true, false, false);
Nico Weber0fca0222008-08-22 09:25:22 +0000175 }
176
177 AddPath("/usr/local/include", System, false, false, false);
178
179 AddPath("/usr/lib/gcc/i686-apple-darwin10/4.2.1/include", System,
180 false, false, false);
181 AddPath("/usr/lib/gcc/powerpc-apple-darwin10/4.2.1/include",
182 System, false, false, false);
183
184 // leopard
185 AddPath("/usr/lib/gcc/i686-apple-darwin9/4.0.1/include", System,
186 false, false, false);
187 AddPath("/usr/lib/gcc/powerpc-apple-darwin9/4.0.1/include",
188 System, false, false, false);
189 AddPath("/usr/lib/gcc/powerpc-apple-darwin9/"
190 "4.0.1/../../../../powerpc-apple-darwin0/include",
191 System, false, false, false);
192
193 // tiger
194 AddPath("/usr/lib/gcc/i686-apple-darwin8/4.0.1/include", System,
195 false, false, false);
196 AddPath("/usr/lib/gcc/powerpc-apple-darwin8/4.0.1/include",
197 System, false, false, false);
198 AddPath("/usr/lib/gcc/powerpc-apple-darwin8/"
199 "4.0.1/../../../../powerpc-apple-darwin8/include",
200 System, false, false, false);
201
202 // Ubuntu 7.10 - Gutsy Gibbon
203 AddPath("/usr/lib/gcc/i486-linux-gnu/4.1.3/include", System,
204 false, false, false);
205
206 // Fedora 8
207 AddPath("/usr/lib/gcc/i386-redhat-linux/4.1.2/include", System,
208 false, false, false);
209
210 // Fedora 9
211 AddPath("/usr/lib/gcc/i386-redhat-linux/4.3.0/include", System,
212 false, false, false);
213
Zhongxing Xu776caef2008-12-25 09:28:01 +0000214 // Fedora 10
215 AddPath("/usr/lib/gcc/i386-redhat-linux/4.3.2/include", System,
216 false, false, false);
217
Nico Weber0fca0222008-08-22 09:25:22 +0000218 //Debian testing/lenny x86
219 AddPath("/usr/lib/gcc/i486-linux-gnu/4.2.3/include", System,
220 false, false, false);
221
222 //Debian testing/lenny amd64
223 AddPath("/usr/lib/gcc/x86_64-linux-gnu/4.2.3/include", System,
224 false, false, false);
225
Torok Edwinf7ea67a2009-02-05 18:12:45 +0000226 // Debian sid amd64
227 AddPath("/usr/lib/gcc/x86_64-linux-gnu/4.3/include", System,
228 false, false, false);
229 AddPath("/usr/lib/gcc/x86_64-linux-gnu/4.3/include-fixed",
230 System, false, false, false);
231
Nico Weber0fca0222008-08-22 09:25:22 +0000232 // Arch Linux 2008-06-24
233 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.1/include", System,
234 false, false, false);
235 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.1/include-fixed", System,
236 false, false, false);
237 AddPath("/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/include", System,
238 false, false, false);
239 AddPath("/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/include-fixed",
240 System, false, false, false);
241
242 // Debian testing/lenny ppc32
243 AddPath("/usr/lib/gcc/powerpc-linux-gnu/4.2.3/include", System,
244 false, false, false);
245
246 // Gentoo x86 stable
247 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include", System,
248 false, false, false);
249
Chris Lattner5654ffd2008-08-23 18:25:07 +0000250 // DragonFly
251 AddPath("/usr/libdata/gcc41", System, true, false, false);
252
Nico Weber0fca0222008-08-22 09:25:22 +0000253 AddPath("/usr/include", System, false, false, false);
254 AddPath("/System/Library/Frameworks", System, true, false, true);
255 AddPath("/Library/Frameworks", System, true, false, true);
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000256
257#endif
Nico Weber0fca0222008-08-22 09:25:22 +0000258}
259
260void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) {
261 AddEnvVarPaths("CPATH");
262 if (Lang.CPlusPlus && Lang.ObjC1)
263 AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH");
264 else if (Lang.CPlusPlus)
265 AddEnvVarPaths("CPLUS_INCLUDE_PATH");
266 else if (Lang.ObjC1)
267 AddEnvVarPaths("OBJC_INCLUDE_PATH");
268 else
269 AddEnvVarPaths("C_INCLUDE_PATH");
270}
271
272
273/// RemoveDuplicates - If there are duplicate directory entries in the specified
274/// search list, remove the later (dead) ones.
275static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
276 bool Verbose) {
277 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
278 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
279 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
280 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000281 unsigned DirToRemove = i;
282
Chris Lattner43eee072009-02-08 01:00:10 +0000283 const DirectoryLookup &CurEntry = SearchList[i];
284
285 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000286 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000287 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000288 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000289 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000290 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000291 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000292 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000293 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000294 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000295 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000296 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000297 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000298 }
299
300 // If we have a normal #include dir/framework/headermap that is shadowed
301 // later in the chain by a system include location, we actually want to
302 // ignore the user's request and drop the user dir... keeping the system
303 // dir. This is weird, but required to emulate GCC's search path correctly.
304 //
305 // Since dupes of system dirs are rare, just rescan to find the original
306 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000307 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000308 // Find the dir that this is the same of.
309 unsigned FirstDir;
310 for (FirstDir = 0; ; ++FirstDir) {
311 assert(FirstDir != i && "Didn't find dupe?");
312
Chris Lattner43eee072009-02-08 01:00:10 +0000313 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
314
Chris Lattner30f05b52009-02-08 00:55:22 +0000315 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000316 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000317 continue;
318
319 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000320 if (CurEntry.isNormalDir())
321 isSame = SearchEntry.getDir() == CurEntry.getDir();
322 else if (CurEntry.isFramework())
323 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000324 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000325 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
326 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000327 }
328
329 if (isSame)
330 break;
331 }
Nico Weber0fca0222008-08-22 09:25:22 +0000332
Chris Lattner30f05b52009-02-08 00:55:22 +0000333 // If the first dir in the search path is a non-system dir, zap it
334 // instead of the system one.
335 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
336 DirToRemove = FirstDir;
337 }
338
339 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000340 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
341 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000342 if (DirToRemove != i)
343 fprintf(stderr, " as it is a non-system directory that duplicates"
344 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000345 }
346
Chris Lattner7a739402008-09-26 17:46:45 +0000347 // This is reached if the current entry is a duplicate. Remove the
348 // DirToRemove (usually the current dir).
349 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000350 --i;
351 }
352}
353
354
355void InitHeaderSearch::Realize() {
356 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
357 std::vector<DirectoryLookup> SearchList;
358 SearchList = IncludeGroup[Angled];
359 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
360 IncludeGroup[System].end());
361 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
362 IncludeGroup[After].end());
363 RemoveDuplicates(SearchList, Verbose);
364 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
365
366 // Prepend QUOTED list on the search list.
367 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
368 IncludeGroup[Quoted].end());
369
370
371 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
372 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
373 DontSearchCurDir);
374
375 // If verbose, print the list of directories that will be searched.
376 if (Verbose) {
377 fprintf(stderr, "#include \"...\" search starts here:\n");
378 unsigned QuotedIdx = IncludeGroup[Quoted].size();
379 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
380 if (i == QuotedIdx)
381 fprintf(stderr, "#include <...> search starts here:\n");
382 const char *Name = SearchList[i].getName();
383 const char *Suffix;
384 if (SearchList[i].isNormalDir())
385 Suffix = "";
386 else if (SearchList[i].isFramework())
387 Suffix = " (framework directory)";
388 else {
389 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
390 Suffix = " (headermap)";
391 }
392 fprintf(stderr, " %s%s\n", Name, Suffix);
393 }
394 fprintf(stderr, "End of search list.\n");
395 }
396}
397