blob: 0b633513808489003bb8c9c0abb1e69d46cc8e9d [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
226 // Arch Linux 2008-06-24
227 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.1/include", System,
228 false, false, false);
229 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.1/include-fixed", System,
230 false, false, false);
231 AddPath("/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/include", System,
232 false, false, false);
233 AddPath("/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/include-fixed",
234 System, false, false, false);
235
236 // Debian testing/lenny ppc32
237 AddPath("/usr/lib/gcc/powerpc-linux-gnu/4.2.3/include", System,
238 false, false, false);
239
240 // Gentoo x86 stable
241 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include", System,
242 false, false, false);
243
Chris Lattner5654ffd2008-08-23 18:25:07 +0000244 // DragonFly
245 AddPath("/usr/libdata/gcc41", System, true, false, false);
246
Nico Weber0fca0222008-08-22 09:25:22 +0000247 AddPath("/usr/include", System, false, false, false);
248 AddPath("/System/Library/Frameworks", System, true, false, true);
249 AddPath("/Library/Frameworks", System, true, false, true);
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000250
251#endif
Nico Weber0fca0222008-08-22 09:25:22 +0000252}
253
254void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) {
255 AddEnvVarPaths("CPATH");
256 if (Lang.CPlusPlus && Lang.ObjC1)
257 AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH");
258 else if (Lang.CPlusPlus)
259 AddEnvVarPaths("CPLUS_INCLUDE_PATH");
260 else if (Lang.ObjC1)
261 AddEnvVarPaths("OBJC_INCLUDE_PATH");
262 else
263 AddEnvVarPaths("C_INCLUDE_PATH");
264}
265
266
267/// RemoveDuplicates - If there are duplicate directory entries in the specified
268/// search list, remove the later (dead) ones.
269static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
270 bool Verbose) {
271 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
272 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
273 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
274 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000275 unsigned DirToRemove = i;
276
Nico Weber0fca0222008-08-22 09:25:22 +0000277 if (SearchList[i].isNormalDir()) {
278 // If this isn't the first time we've seen this dir, remove it.
279 if (SeenDirs.insert(SearchList[i].getDir()))
280 continue;
281
Chris Lattner7a739402008-09-26 17:46:45 +0000282 // If we have a normal #include dir that is shadowed later in the chain
283 // by a system include dir, we actually want to ignore the user's request
284 // and drop the user dir... keeping the system dir. This is weird, but
285 // required to emulate GCC's search path correctly.
286 //
287 // Since dupes of system dirs are rare, just rescan to find the original
288 // that we're nuking instead of using a DenseMap.
Chris Lattner0b9e7362008-09-26 21:18:42 +0000289 if (SearchList[i].getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner7a739402008-09-26 17:46:45 +0000290 // Find the dir that this is the same of.
291 unsigned FirstDir;
292 for (FirstDir = 0; ; ++FirstDir) {
293 assert(FirstDir != i && "Didn't find dupe?");
294 if (SearchList[FirstDir].getDir() == SearchList[i].getDir())
295 break;
296 }
297
298 // If the first dir in the search path is a non-system dir, zap it
299 // instead of the system one.
Chris Lattner0b9e7362008-09-26 21:18:42 +0000300 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
Chris Lattner7a739402008-09-26 17:46:45 +0000301 DirToRemove = FirstDir;
302 }
303
304 if (Verbose) {
Nico Weber0fca0222008-08-22 09:25:22 +0000305 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
306 SearchList[i].getDir()->getName());
Chris Lattner7a739402008-09-26 17:46:45 +0000307 if (DirToRemove != i)
308 fprintf(stderr, " as it is a non-system directory that duplicates"
309 " a system directory\n");
310 }
Nico Weber0fca0222008-08-22 09:25:22 +0000311 } else if (SearchList[i].isFramework()) {
312 // If this isn't the first time we've seen this framework dir, remove it.
313 if (SeenFrameworkDirs.insert(SearchList[i].getFrameworkDir()))
314 continue;
315
316 if (Verbose)
317 fprintf(stderr, "ignoring duplicate framework \"%s\"\n",
318 SearchList[i].getFrameworkDir()->getName());
319
320 } else {
321 assert(SearchList[i].isHeaderMap() && "Not a headermap or normal dir?");
322 // If this isn't the first time we've seen this headermap, remove it.
323 if (SeenHeaderMaps.insert(SearchList[i].getHeaderMap()))
324 continue;
325
326 if (Verbose)
327 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
328 SearchList[i].getDir()->getName());
329 }
330
Chris Lattner7a739402008-09-26 17:46:45 +0000331 // This is reached if the current entry is a duplicate. Remove the
332 // DirToRemove (usually the current dir).
333 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000334 --i;
335 }
336}
337
338
339void InitHeaderSearch::Realize() {
340 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
341 std::vector<DirectoryLookup> SearchList;
342 SearchList = IncludeGroup[Angled];
343 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
344 IncludeGroup[System].end());
345 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
346 IncludeGroup[After].end());
347 RemoveDuplicates(SearchList, Verbose);
348 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
349
350 // Prepend QUOTED list on the search list.
351 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
352 IncludeGroup[Quoted].end());
353
354
355 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
356 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
357 DontSearchCurDir);
358
359 // If verbose, print the list of directories that will be searched.
360 if (Verbose) {
361 fprintf(stderr, "#include \"...\" search starts here:\n");
362 unsigned QuotedIdx = IncludeGroup[Quoted].size();
363 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
364 if (i == QuotedIdx)
365 fprintf(stderr, "#include <...> search starts here:\n");
366 const char *Name = SearchList[i].getName();
367 const char *Suffix;
368 if (SearchList[i].isNormalDir())
369 Suffix = "";
370 else if (SearchList[i].isFramework())
371 Suffix = " (framework directory)";
372 else {
373 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
374 Suffix = " (headermap)";
375 }
376 fprintf(stderr, " %s%s\n", Name, Suffix);
377 }
378 fprintf(stderr, "End of search list.\n");
379 }
380}
381