blob: 7fd36f80e97f2eb56ae6e117ef2f423cd60a86cd [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"
15
16#include "clang/Lex/HeaderSearch.h"
17#include "clang/Basic/FileManager.h"
18#include "clang/Basic/LangOptions.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/System/Path.h"
22
23#include <vector>
24
25using namespace clang;
26
27void InitHeaderSearch::AddPath(const std::string &Path, IncludeDirGroup Group,
28 bool isCXXAware, bool isUserSupplied,
29 bool isFramework) {
30 assert(!Path.empty() && "can't handle empty path here");
31 FileManager &FM = Headers.getFileMgr();
32
33 // Compute the actual path, taking into consideration -isysroot.
34 llvm::SmallString<256> MappedPath;
35
36 // Handle isysroot.
37 if (Group == System) {
38 // 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 }
43
44 MappedPath.append(Path.begin(), Path.end());
45
46 // Compute the DirectoryLookup type.
47 DirectoryLookup::DirType Type;
48 if (Group == Quoted || Group == Angled)
49 Type = DirectoryLookup::NormalHeaderDir;
50 else if (isCXXAware)
51 Type = DirectoryLookup::SystemHeaderDir;
52 else
53 Type = DirectoryLookup::ExternCSystemHeaderDir;
54
55
56 // If the directory exists, add it.
57 if (const DirectoryEntry *DE = FM.getDirectory(&MappedPath[0],
58 &MappedPath[0]+
59 MappedPath.size())) {
60 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
61 isFramework));
62 return;
63 }
64
65 // Check to see if this is an apple-style headermap (which are not allowed to
66 // be frameworks).
67 if (!isFramework) {
68 if (const FileEntry *FE = FM.getFile(&MappedPath[0],
69 &MappedPath[0]+MappedPath.size())) {
70 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
71 // It is a headermap, add it to the search path.
72 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
73 return;
74 }
75 }
76 }
77
78 if (Verbose)
79 fprintf(stderr, "ignoring nonexistent directory \"%s\"\n", Path.c_str());
80}
81
82
83void InitHeaderSearch::AddEnvVarPaths(const char *Name) {
84 const char* at = getenv(Name);
85 if (!at)
86 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,
94 true, false);
95 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?
108 if (Lang.CPlusPlus) {
109 AddPath("/usr/include/c++/4.2.1", System, true, false, false);
110 AddPath("/usr/include/c++/4.2.1/i686-apple-darwin10", System, true, false,
111 false);
112 AddPath("/usr/include/c++/4.2.1/backward", System, true, false, false);
113
114 AddPath("/usr/include/c++/4.0.0", System, true, false, false);
115 AddPath("/usr/include/c++/4.0.0/i686-apple-darwin8", System, true, false,
116 false);
117 AddPath("/usr/include/c++/4.0.0/backward", System, true, false, false);
118
119 // Ubuntu 7.10 - Gutsy Gibbon
120 AddPath("/usr/include/c++/4.1.3", System, true, false, false);
121 AddPath("/usr/include/c++/4.1.3/i486-linux-gnu", System, true, false,
122 false);
123 AddPath("/usr/include/c++/4.1.3/backward", System, true, false, false);
124
125 // Fedora 8
126 AddPath("/usr/include/c++/4.1.2", System, true, false, false);
127 AddPath("/usr/include/c++/4.1.2/i386-redhat-linux", System, true, false,
128 false);
129 AddPath("/usr/include/c++/4.1.2/backward", System, true, false, false);
130
131 // Fedora 9
132 AddPath("/usr/include/c++/4.3.0", System, true, false, false);
133 AddPath("/usr/include/c++/4.3.0/i386-redhat-linux", System, true, false,
134 false);
135 AddPath("/usr/include/c++/4.3.0/backward", System, true, false, false);
136
137 // Arch Linux 2008-06-24
138 AddPath("/usr/include/c++/4.3.1", System, true, false, false);
139 AddPath("/usr/include/c++/4.3.1/i686-pc-linux-gnu", System, true, false,
140 false);
141 AddPath("/usr/include/c++/4.3.1/backward", System, true, false, false);
142 AddPath("/usr/include/c++/4.3.1/x86_64-unknown-linux-gnu", System, true,
143 false, false);
144 }
145
146 AddPath("/usr/local/include", System, false, false, false);
147
148 AddPath("/usr/lib/gcc/i686-apple-darwin10/4.2.1/include", System,
149 false, false, false);
150 AddPath("/usr/lib/gcc/powerpc-apple-darwin10/4.2.1/include",
151 System, false, false, false);
152
153 // leopard
154 AddPath("/usr/lib/gcc/i686-apple-darwin9/4.0.1/include", System,
155 false, false, false);
156 AddPath("/usr/lib/gcc/powerpc-apple-darwin9/4.0.1/include",
157 System, false, false, false);
158 AddPath("/usr/lib/gcc/powerpc-apple-darwin9/"
159 "4.0.1/../../../../powerpc-apple-darwin0/include",
160 System, false, false, false);
161
162 // tiger
163 AddPath("/usr/lib/gcc/i686-apple-darwin8/4.0.1/include", System,
164 false, false, false);
165 AddPath("/usr/lib/gcc/powerpc-apple-darwin8/4.0.1/include",
166 System, false, false, false);
167 AddPath("/usr/lib/gcc/powerpc-apple-darwin8/"
168 "4.0.1/../../../../powerpc-apple-darwin8/include",
169 System, false, false, false);
170
171 // Ubuntu 7.10 - Gutsy Gibbon
172 AddPath("/usr/lib/gcc/i486-linux-gnu/4.1.3/include", System,
173 false, false, false);
174
175 // Fedora 8
176 AddPath("/usr/lib/gcc/i386-redhat-linux/4.1.2/include", System,
177 false, false, false);
178
179 // Fedora 9
180 AddPath("/usr/lib/gcc/i386-redhat-linux/4.3.0/include", System,
181 false, false, false);
182
183 //Debian testing/lenny x86
184 AddPath("/usr/lib/gcc/i486-linux-gnu/4.2.3/include", System,
185 false, false, false);
186
187 //Debian testing/lenny amd64
188 AddPath("/usr/lib/gcc/x86_64-linux-gnu/4.2.3/include", System,
189 false, false, false);
190
191 // Arch Linux 2008-06-24
192 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.1/include", System,
193 false, false, false);
194 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.3.1/include-fixed", System,
195 false, false, false);
196 AddPath("/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/include", System,
197 false, false, false);
198 AddPath("/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/include-fixed",
199 System, false, false, false);
200
201 // Debian testing/lenny ppc32
202 AddPath("/usr/lib/gcc/powerpc-linux-gnu/4.2.3/include", System,
203 false, false, false);
204
205 // Gentoo x86 stable
206 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include", System,
207 false, false, false);
208
209 AddPath("/usr/include", System, false, false, false);
210 AddPath("/System/Library/Frameworks", System, true, false, true);
211 AddPath("/Library/Frameworks", System, true, false, true);
212}
213
214void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) {
215 AddEnvVarPaths("CPATH");
216 if (Lang.CPlusPlus && Lang.ObjC1)
217 AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH");
218 else if (Lang.CPlusPlus)
219 AddEnvVarPaths("CPLUS_INCLUDE_PATH");
220 else if (Lang.ObjC1)
221 AddEnvVarPaths("OBJC_INCLUDE_PATH");
222 else
223 AddEnvVarPaths("C_INCLUDE_PATH");
224}
225
226
227/// RemoveDuplicates - If there are duplicate directory entries in the specified
228/// search list, remove the later (dead) ones.
229static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
230 bool Verbose) {
231 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
232 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
233 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
234 for (unsigned i = 0; i != SearchList.size(); ++i) {
235 if (SearchList[i].isNormalDir()) {
236 // If this isn't the first time we've seen this dir, remove it.
237 if (SeenDirs.insert(SearchList[i].getDir()))
238 continue;
239
240 if (Verbose)
241 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
242 SearchList[i].getDir()->getName());
243 } else if (SearchList[i].isFramework()) {
244 // If this isn't the first time we've seen this framework dir, remove it.
245 if (SeenFrameworkDirs.insert(SearchList[i].getFrameworkDir()))
246 continue;
247
248 if (Verbose)
249 fprintf(stderr, "ignoring duplicate framework \"%s\"\n",
250 SearchList[i].getFrameworkDir()->getName());
251
252 } else {
253 assert(SearchList[i].isHeaderMap() && "Not a headermap or normal dir?");
254 // If this isn't the first time we've seen this headermap, remove it.
255 if (SeenHeaderMaps.insert(SearchList[i].getHeaderMap()))
256 continue;
257
258 if (Verbose)
259 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
260 SearchList[i].getDir()->getName());
261 }
262
263 // This is reached if the current entry is a duplicate.
264 SearchList.erase(SearchList.begin()+i);
265 --i;
266 }
267}
268
269
270void InitHeaderSearch::Realize() {
271 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
272 std::vector<DirectoryLookup> SearchList;
273 SearchList = IncludeGroup[Angled];
274 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
275 IncludeGroup[System].end());
276 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
277 IncludeGroup[After].end());
278 RemoveDuplicates(SearchList, Verbose);
279 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
280
281 // Prepend QUOTED list on the search list.
282 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
283 IncludeGroup[Quoted].end());
284
285
286 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
287 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
288 DontSearchCurDir);
289
290 // If verbose, print the list of directories that will be searched.
291 if (Verbose) {
292 fprintf(stderr, "#include \"...\" search starts here:\n");
293 unsigned QuotedIdx = IncludeGroup[Quoted].size();
294 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
295 if (i == QuotedIdx)
296 fprintf(stderr, "#include <...> search starts here:\n");
297 const char *Name = SearchList[i].getName();
298 const char *Suffix;
299 if (SearchList[i].isNormalDir())
300 Suffix = "";
301 else if (SearchList[i].isFramework())
302 Suffix = " (framework directory)";
303 else {
304 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
305 Suffix = " (headermap)";
306 }
307 fprintf(stderr, " %s%s\n", Name, Suffix);
308 }
309 fprintf(stderr, "End of search list.\n");
310 }
311}
312