blob: 5b0b2c2f72be6bed15dbf78ecfc19dc940be7705 [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>
Nico Weber0fca0222008-08-22 09:25:22 +000023using namespace clang;
24
25void InitHeaderSearch::AddPath(const std::string &Path, IncludeDirGroup Group,
26 bool isCXXAware, bool isUserSupplied,
Chris Lattner6858dd32009-02-19 06:48:28 +000027 bool isFramework, bool IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000028 assert(!Path.empty() && "can't handle empty path here");
29 FileManager &FM = Headers.getFileMgr();
30
31 // Compute the actual path, taking into consideration -isysroot.
32 llvm::SmallString<256> MappedPath;
33
34 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +000035 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000036 // FIXME: Portability. This should be a sys::Path interface, this doesn't
37 // handle things like C:\ right, nor win32 \\network\device\blah.
38 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
39 MappedPath.append(isysroot.begin(), isysroot.end());
40 }
41
42 MappedPath.append(Path.begin(), Path.end());
43
44 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +000045 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +000046 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +000047 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +000048 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +000049 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +000050 else
Chris Lattner0b9e7362008-09-26 21:18:42 +000051 Type = SrcMgr::C_ExternCSystem;
Nico Weber0fca0222008-08-22 09:25:22 +000052
53
54 // If the directory exists, add it.
55 if (const DirectoryEntry *DE = FM.getDirectory(&MappedPath[0],
56 &MappedPath[0]+
57 MappedPath.size())) {
58 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
59 isFramework));
60 return;
61 }
62
63 // Check to see if this is an apple-style headermap (which are not allowed to
64 // be frameworks).
65 if (!isFramework) {
66 if (const FileEntry *FE = FM.getFile(&MappedPath[0],
67 &MappedPath[0]+MappedPath.size())) {
68 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
69 // It is a headermap, add it to the search path.
70 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
71 return;
72 }
73 }
74 }
75
76 if (Verbose)
Chris Lattner50b587d2009-02-19 04:48:57 +000077 fprintf(stderr, "ignoring nonexistent directory \"%s\"\n",
78 MappedPath.c_str());
Nico Weber0fca0222008-08-22 09:25:22 +000079}
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
Chris Lattner6f541022009-02-18 00:25:15 +0000111 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++",
112 System, true, false, false);
113 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/mingw32",
114 System, true, false, false);
115 AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/backward",
116 System, true, false, false);
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000117 }
118
119 // Mingw32 GCC version 4
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000120 AddPath("C:/mingw/include", System, false, false, false);
121#else
122
Nico Weber0fca0222008-08-22 09:25:22 +0000123 if (Lang.CPlusPlus) {
124 AddPath("/usr/include/c++/4.2.1", System, true, false, false);
125 AddPath("/usr/include/c++/4.2.1/i686-apple-darwin10", System, true, false,
126 false);
127 AddPath("/usr/include/c++/4.2.1/backward", System, true, false, false);
128
129 AddPath("/usr/include/c++/4.0.0", System, true, false, false);
130 AddPath("/usr/include/c++/4.0.0/i686-apple-darwin8", System, true, false,
131 false);
132 AddPath("/usr/include/c++/4.0.0/backward", System, true, false, false);
133
134 // Ubuntu 7.10 - Gutsy Gibbon
135 AddPath("/usr/include/c++/4.1.3", System, true, false, false);
136 AddPath("/usr/include/c++/4.1.3/i486-linux-gnu", System, true, false,
137 false);
138 AddPath("/usr/include/c++/4.1.3/backward", System, true, false, false);
139
140 // Fedora 8
141 AddPath("/usr/include/c++/4.1.2", System, true, false, false);
142 AddPath("/usr/include/c++/4.1.2/i386-redhat-linux", System, true, false,
143 false);
144 AddPath("/usr/include/c++/4.1.2/backward", System, true, false, false);
145
146 // Fedora 9
147 AddPath("/usr/include/c++/4.3.0", System, true, false, false);
148 AddPath("/usr/include/c++/4.3.0/i386-redhat-linux", System, true, false,
149 false);
150 AddPath("/usr/include/c++/4.3.0/backward", System, true, false, false);
151
Zhongxing Xu776caef2008-12-25 09:28:01 +0000152 // Fedora 10
153 AddPath("/usr/include/c++/4.3.2", System, true, false, false);
154 AddPath("/usr/include/c++/4.3.2/i386-redhat-linux", System, true, false,
155 false);
156 AddPath("/usr/include/c++/4.3.2/backward", System, true, false, false);
157
Nico Weber0fca0222008-08-22 09:25:22 +0000158 // Arch Linux 2008-06-24
159 AddPath("/usr/include/c++/4.3.1", System, true, false, false);
160 AddPath("/usr/include/c++/4.3.1/i686-pc-linux-gnu", System, true, false,
161 false);
162 AddPath("/usr/include/c++/4.3.1/backward", System, true, false, false);
163 AddPath("/usr/include/c++/4.3.1/x86_64-unknown-linux-gnu", System, true,
164 false, false);
Chris Lattner5654ffd2008-08-23 18:25:07 +0000165
Nuno Lopesa3d783b2008-12-07 12:11:37 +0000166 // Gentoo x86 stable
167 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4", System,
168 true, false, false);
169 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/"
170 "i686-pc-linux-gnu", System, true, false, false);
171 AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/backward",
172 System, true, false, false);
173
174
Chris Lattner5654ffd2008-08-23 18:25:07 +0000175 // DragonFly
176 AddPath("/usr/include/c++/4.1", System, true, false, false);
Nico Weber0fca0222008-08-22 09:25:22 +0000177 }
178
179 AddPath("/usr/local/include", System, false, false, false);
180
Nico Weber0fca0222008-08-22 09:25:22 +0000181 AddPath("/usr/include", System, false, false, false);
182 AddPath("/System/Library/Frameworks", System, true, false, true);
183 AddPath("/Library/Frameworks", System, true, false, true);
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000184#endif
Nico Weber0fca0222008-08-22 09:25:22 +0000185}
186
187void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) {
188 AddEnvVarPaths("CPATH");
189 if (Lang.CPlusPlus && Lang.ObjC1)
190 AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH");
191 else if (Lang.CPlusPlus)
192 AddEnvVarPaths("CPLUS_INCLUDE_PATH");
193 else if (Lang.ObjC1)
194 AddEnvVarPaths("OBJC_INCLUDE_PATH");
195 else
196 AddEnvVarPaths("C_INCLUDE_PATH");
197}
198
199
200/// RemoveDuplicates - If there are duplicate directory entries in the specified
201/// search list, remove the later (dead) ones.
202static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
203 bool Verbose) {
204 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
205 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
206 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
207 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000208 unsigned DirToRemove = i;
209
Chris Lattner43eee072009-02-08 01:00:10 +0000210 const DirectoryLookup &CurEntry = SearchList[i];
211
212 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000213 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000214 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000215 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000216 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000217 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000218 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000219 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000220 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000221 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000222 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000223 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000224 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000225 }
226
227 // If we have a normal #include dir/framework/headermap that is shadowed
228 // later in the chain by a system include location, we actually want to
229 // ignore the user's request and drop the user dir... keeping the system
230 // dir. This is weird, but required to emulate GCC's search path correctly.
231 //
232 // Since dupes of system dirs are rare, just rescan to find the original
233 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000234 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000235 // Find the dir that this is the same of.
236 unsigned FirstDir;
237 for (FirstDir = 0; ; ++FirstDir) {
238 assert(FirstDir != i && "Didn't find dupe?");
239
Chris Lattner43eee072009-02-08 01:00:10 +0000240 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
241
Chris Lattner30f05b52009-02-08 00:55:22 +0000242 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000243 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000244 continue;
245
246 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000247 if (CurEntry.isNormalDir())
248 isSame = SearchEntry.getDir() == CurEntry.getDir();
249 else if (CurEntry.isFramework())
250 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000251 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000252 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
253 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000254 }
255
256 if (isSame)
257 break;
258 }
Nico Weber0fca0222008-08-22 09:25:22 +0000259
Chris Lattner30f05b52009-02-08 00:55:22 +0000260 // If the first dir in the search path is a non-system dir, zap it
261 // instead of the system one.
262 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
263 DirToRemove = FirstDir;
264 }
265
266 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000267 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
268 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000269 if (DirToRemove != i)
270 fprintf(stderr, " as it is a non-system directory that duplicates"
271 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000272 }
273
Chris Lattner7a739402008-09-26 17:46:45 +0000274 // This is reached if the current entry is a duplicate. Remove the
275 // DirToRemove (usually the current dir).
276 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000277 --i;
278 }
279}
280
281
282void InitHeaderSearch::Realize() {
283 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
284 std::vector<DirectoryLookup> SearchList;
285 SearchList = IncludeGroup[Angled];
286 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
287 IncludeGroup[System].end());
288 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
289 IncludeGroup[After].end());
290 RemoveDuplicates(SearchList, Verbose);
291 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
292
293 // Prepend QUOTED list on the search list.
294 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
295 IncludeGroup[Quoted].end());
296
297
298 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
299 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
300 DontSearchCurDir);
301
302 // If verbose, print the list of directories that will be searched.
303 if (Verbose) {
304 fprintf(stderr, "#include \"...\" search starts here:\n");
305 unsigned QuotedIdx = IncludeGroup[Quoted].size();
306 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
307 if (i == QuotedIdx)
308 fprintf(stderr, "#include <...> search starts here:\n");
309 const char *Name = SearchList[i].getName();
310 const char *Suffix;
311 if (SearchList[i].isNormalDir())
312 Suffix = "";
313 else if (SearchList[i].isFramework())
314 Suffix = " (framework directory)";
315 else {
316 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
317 Suffix = " (headermap)";
318 }
319 fprintf(stderr, " %s%s\n", Name, Suffix);
320 }
321 fprintf(stderr, "End of search list.\n");
322 }
323}
324