blob: a40a569d92f783b57a0e7c49b3c184cc1b14fc8e [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 Dunbar2cdafa82009-11-09 23:02:47 +000014#include "clang/Frontend/Utils.h"
Nico Weber0fca0222008-08-22 09:25:22 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/LangOptions.h"
Daniel Dunbar63c8b772009-11-07 04:20:50 +000017#include "clang/Frontend/HeaderSearchOptions.h"
18#include "clang/Lex/HeaderSearch.h"
Nico Weber0fca0222008-08-22 09:25:22 +000019#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/SmallPtrSet.h"
Rafael Espindolaaadd7a42009-11-13 05:13:58 +000021#include "llvm/ADT/SmallVector.h"
Rafael Espindolaf0a2f512009-11-12 05:48:41 +000022#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000023#include "llvm/ADT/Triple.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000024#include "llvm/Support/raw_ostream.h"
Nico Weber0fca0222008-08-22 09:25:22 +000025#include "llvm/System/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000026#include "llvm/Config/config.h"
Chris Lattner3daed522009-03-02 22:20:04 +000027#include <cstdio>
Mike Stump620d57a2009-10-12 20:50:45 +000028#ifdef _MSC_VER
29 #define WIN32_LEAN_AND_MEAN 1
30 #include <windows.h>
31#endif
Nico Weber0fca0222008-08-22 09:25:22 +000032using namespace clang;
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000033using namespace clang::frontend;
34
35namespace {
36
37/// InitHeaderSearch - This class makes it easier to set the search paths of
38/// a HeaderSearch object. InitHeaderSearch stores several search path lists
39/// internally, which can be sent to a HeaderSearch object in one swoop.
40class InitHeaderSearch {
41 std::vector<DirectoryLookup> IncludeGroup[4];
42 HeaderSearch& Headers;
43 bool Verbose;
44 std::string isysroot;
45
46public:
47
48 InitHeaderSearch(HeaderSearch &HS,
49 bool verbose = false, const std::string &iSysroot = "")
50 : Headers(HS), Verbose(verbose), isysroot(iSysroot) {}
51
52 /// AddPath - Add the specified path to the specified group list.
53 void AddPath(const llvm::StringRef &Path, IncludeDirGroup Group,
54 bool isCXXAware, bool isUserSupplied,
55 bool isFramework, bool IgnoreSysRoot = false);
56
57 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to suport a gnu
58 /// libstdc++.
Rafael Espindolaab7ae952009-11-16 19:49:37 +000059 void AddGnuCPlusPlusIncludePaths(const std::string &Base,
60 const char *ArchDir,
61 const char *Dir32,
Daniel Dunbar2cdafa82009-11-09 23:02:47 +000062 const char *Dir64,
63 const llvm::Triple &triple);
64
65 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW
66 /// libstdc++.
67 void AddMinGWCPlusPlusIncludePaths(const std::string &Base,
68 const char *Arch,
69 const char *Version);
70
71 /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
72 /// separator. The processing follows that of the CPATH variable for gcc.
73 void AddDelimitedPaths(const char *String);
74
75 // AddDefaultCIncludePaths - Add paths that should always be searched.
76 void AddDefaultCIncludePaths(const llvm::Triple &triple);
77
78 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
79 // compiling c++.
80 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
81
82 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
83 /// that e.g. stdio.h is found.
84 void AddDefaultSystemIncludePaths(const LangOptions &Lang,
85 const llvm::Triple &triple);
86
87 /// Realize - Merges all search path lists into one list and send it to
88 /// HeaderSearch.
89 void Realize();
90};
91
92}
Nico Weber0fca0222008-08-22 09:25:22 +000093
Benjamin Kramer458fb102009-09-05 09:49:39 +000094void InitHeaderSearch::AddPath(const llvm::StringRef &Path,
95 IncludeDirGroup Group, bool isCXXAware,
96 bool isUserSupplied, bool isFramework,
97 bool IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000098 assert(!Path.empty() && "can't handle empty path here");
99 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Nico Weber0fca0222008-08-22 09:25:22 +0000101 // Compute the actual path, taking into consideration -isysroot.
102 llvm::SmallString<256> MappedPath;
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Nico Weber0fca0222008-08-22 09:25:22 +0000104 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +0000105 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +0000106 // FIXME: Portability. This should be a sys::Path interface, this doesn't
107 // handle things like C:\ right, nor win32 \\network\device\blah.
108 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
109 MappedPath.append(isysroot.begin(), isysroot.end());
110 }
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Nico Weber0fca0222008-08-22 09:25:22 +0000112 MappedPath.append(Path.begin(), Path.end());
113
114 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +0000115 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +0000116 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000117 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +0000118 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +0000119 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +0000120 else
Chris Lattner0b9e7362008-09-26 21:18:42 +0000121 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +0000122
123
Nico Weber0fca0222008-08-22 09:25:22 +0000124 // If the directory exists, add it.
Benjamin Kramer458fb102009-09-05 09:49:39 +0000125 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000126 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
127 isFramework));
128 return;
129 }
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Nico Weber0fca0222008-08-22 09:25:22 +0000131 // Check to see if this is an apple-style headermap (which are not allowed to
132 // be frameworks).
133 if (!isFramework) {
Benjamin Kramer458fb102009-09-05 09:49:39 +0000134 if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +0000135 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
136 // It is a headermap, add it to the search path.
137 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
138 return;
139 }
140 }
141 }
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Nico Weber0fca0222008-08-22 09:25:22 +0000143 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +0000144 llvm::errs() << "ignoring nonexistent directory \""
145 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +0000146}
147
148
Daniel Dunbare1665822009-11-07 04:20:39 +0000149void InitHeaderSearch::AddDelimitedPaths(const char *at) {
150 if (*at == 0) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +0000151 return;
152
153 const char* delim = strchr(at, llvm::sys::PathSeparator);
154 while (delim != 0) {
155 if (delim-at == 0)
156 AddPath(".", Angled, false, true, false);
157 else
Benjamin Kramer458fb102009-09-05 09:49:39 +0000158 AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false);
Nico Weber0fca0222008-08-22 09:25:22 +0000159 at = delim + 1;
160 delim = strchr(at, llvm::sys::PathSeparator);
161 }
162 if (*at == 0)
163 AddPath(".", Angled, false, true, false);
164 else
165 AddPath(at, Angled, false, true, false);
166}
167
Mike Stumpec057662009-10-09 20:16:49 +0000168void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(const std::string &Base,
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000169 const char *ArchDir,
Rafael Espindola31b63be2009-10-14 17:09:44 +0000170 const char *Dir32,
171 const char *Dir64,
172 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000173 // Add the base dir
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000174 AddPath(Base, System, true, false, false);
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000175
176 // Add the multilib dirs
Rafael Espindola31b63be2009-10-14 17:09:44 +0000177 llvm::Triple::ArchType arch = triple.getArch();
178 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola31b63be2009-10-14 17:09:44 +0000179 if (is64bit)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000180 AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false);
Rafael Espindola31b63be2009-10-14 17:09:44 +0000181 else
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000182 AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false);
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000183
184 // Add the backward dir
185 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000186}
Nico Weber0fca0222008-08-22 09:25:22 +0000187
Mike Stump620d57a2009-10-12 20:50:45 +0000188void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(const std::string &Base,
189 const char *Arch,
190 const char *Version) {
Daniel Dunbar5c5758b2009-11-07 04:20:25 +0000191 std::string localBase = Base + "/" + Arch + "/" + Version + "/include";
192 AddPath(localBase, System, true, false, false);
193 AddPath(localBase + "/c++", System, true, false, false);
194 AddPath(localBase + "/c++/backward", System, true, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000195}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000196
Mike Stump620d57a2009-10-12 20:50:45 +0000197 // FIXME: This probably should goto to some platform utils place.
198#ifdef _MSC_VER
John Thompson75ee3bd2009-11-21 00:15:52 +0000199
Mike Stump620d57a2009-10-12 20:50:45 +0000200 // Read registry string.
John Thompson75ee3bd2009-11-21 00:15:52 +0000201 // This also supports a means to look for high-versioned keys by use
202 // of a $VERSION placeholder in the key path.
203 // $VERSION in the key path is a placeholder for the version number,
204 // causing the highest value path to be searched for and used.
205 // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
206 // There can be additional characters in the component. Only the numberic
207 // characters are compared.
Mike Stump620d57a2009-10-12 20:50:45 +0000208bool getSystemRegistryString(const char *keyPath, const char *valueName,
Mike Stump43d81762009-10-08 23:29:47 +0000209 char *value, size_t maxLength) {
210 HKEY hRootKey = NULL;
211 HKEY hKey = NULL;
212 const char* subKey = NULL;
213 DWORD valueType;
214 DWORD valueSize = maxLength - 1;
John Thompson75ee3bd2009-11-21 00:15:52 +0000215 long lResult;
Mike Stump43d81762009-10-08 23:29:47 +0000216 bool returnValue = false;
217 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
218 hRootKey = HKEY_CLASSES_ROOT;
219 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000220 }
Mike Stump43d81762009-10-08 23:29:47 +0000221 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
222 hRootKey = HKEY_USERS;
223 subKey = keyPath + 11;
224 }
225 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
226 hRootKey = HKEY_LOCAL_MACHINE;
227 subKey = keyPath + 19;
228 }
229 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
230 hRootKey = HKEY_CURRENT_USER;
231 subKey = keyPath + 18;
232 }
233 else
234 return(false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000235 const char *placeHolder = strstr(subKey, "$VERSION");
236 char bestName[256];
237 bestName[0] = '\0';
238 // If we have a $VERSION placeholder, do the highest-version search.
239 if (placeHolder) {
240 const char *keyEnd = placeHolder - 1;
241 const char *nextKey = placeHolder;
242 // Find end of previous key.
243 while ((keyEnd > subKey) && (*keyEnd != '\\'))
244 keyEnd--;
245 // Find end of key containing $VERSION.
246 while (*nextKey && (*nextKey != '\\'))
247 nextKey++;
248 size_t partialKeyLength = keyEnd - subKey;
249 char partialKey[256];
250 if (partialKeyLength > sizeof(partialKey))
251 partialKeyLength = sizeof(partialKey);
252 strncpy(partialKey, subKey, partialKeyLength);
253 partialKey[partialKeyLength] = '\0';
254 HKEY hTopKey = NULL;
255 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
256 if (lResult == ERROR_SUCCESS) {
257 char keyName[256];
258 int bestIndex = -1;
259 double bestValue = 0.0;
260 DWORD index, size = sizeof(keyName) - 1;
261 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
262 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
263 const char *sp = keyName;
264 while (*sp && !isdigit(*sp))
265 sp++;
266 if (!*sp)
267 continue;
268 const char *ep = sp + 1;
269 while (*ep && (isdigit(*ep) || (*ep == '.')))
270 ep++;
271 char numBuf[32];
272 strncpy(numBuf, sp, sizeof(numBuf) - 1);
273 numBuf[sizeof(numBuf) - 1] = '\0';
274 double value = strtod(numBuf, NULL);
275 if (value > bestValue) {
276 bestIndex = (int)index;
277 bestValue = value;
278 strcpy(bestName, keyName);
279 }
280 size = sizeof(keyName) - 1;
281 }
282 // If we found the highest versioned key, open the key and get the value.
283 if (bestIndex != -1) {
284 // Append rest of key.
285 strncat(bestName, nextKey, sizeof(bestName) - 1);
286 bestName[sizeof(bestName) - 1] = '\0';
287 // Open the chosen key path remainder.
288 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
289 if (lResult == ERROR_SUCCESS) {
290 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
291 (LPBYTE)value, &valueSize);
292 if (lResult == ERROR_SUCCESS)
293 returnValue = true;
294 RegCloseKey(hKey);
295 }
296 }
297 RegCloseKey(hTopKey);
298 }
299 }
300 else {
301 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
302 if (lResult == ERROR_SUCCESS) {
303 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
304 (LPBYTE)value, &valueSize);
305 if (lResult == ERROR_SUCCESS)
306 returnValue = true;
307 RegCloseKey(hKey);
308 }
Mike Stump43d81762009-10-08 23:29:47 +0000309 }
310 return(returnValue);
311}
Mike Stump620d57a2009-10-12 20:50:45 +0000312#else // _MSC_VER
313 // Read registry string.
314bool getSystemRegistryString(const char *, const char *, char *, size_t) {
315 return(false);
316}
317#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000318
Mike Stump43d81762009-10-08 23:29:47 +0000319 // Get Visual Studio installation directory.
320bool getVisualStudioDir(std::string &path) {
John Thompson75ee3bd2009-11-21 00:15:52 +0000321 char vsIDEInstallDir[256];
Mike Stump620d57a2009-10-12 20:50:45 +0000322 // Try the Windows registry first.
John Thompson75ee3bd2009-11-21 00:15:52 +0000323 bool hasVCDir = getSystemRegistryString(
324 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
325 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000326 // If we have both vc80 and vc90, pick version we were compiled with.
John Thompson75ee3bd2009-11-21 00:15:52 +0000327 if (hasVCDir && vsIDEInstallDir[0]) {
Mike Stump620d57a2009-10-12 20:50:45 +0000328 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
329 if (p)
330 *p = '\0';
331 path = vsIDEInstallDir;
332 return(true);
333 }
334 else {
335 // Try the environment.
336 const char* vs90comntools = getenv("VS90COMNTOOLS");
337 const char* vs80comntools = getenv("VS80COMNTOOLS");
338 const char* vscomntools = NULL;
339 // If we have both vc80 and vc90, pick version we were compiled with.
340 if (vs90comntools && vs80comntools) {
341 #if (_MSC_VER >= 1500) // VC90
Mike Stump43d81762009-10-08 23:29:47 +0000342 vscomntools = vs90comntools;
343 #elif (_MSC_VER == 1400) // VC80
344 vscomntools = vs80comntools;
345 #else
346 vscomntools = vs90comntools;
347 #endif
Mike Stump620d57a2009-10-12 20:50:45 +0000348 }
349 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000350 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000351 else if (vs80comntools)
352 vscomntools = vs80comntools;
353 if (vscomntools && *vscomntools) {
354 char *p = (char*)strstr(vscomntools, "\\Common7\\Tools");
355 if (p)
356 *p = '\0';
357 path = vscomntools;
358 return(true);
359 }
360 else
361 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000362 }
Mike Stump620d57a2009-10-12 20:50:45 +0000363 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000364}
Mike Stump43d81762009-10-08 23:29:47 +0000365
John Thompson75ee3bd2009-11-21 00:15:52 +0000366 // Get Windows SDK installation directory.
367bool getWindowsSDKDir(std::string &path) {
368 char windowsSDKInstallDir[256];
369 // Try the Windows registry.
370 bool hasSDKDir = getSystemRegistryString(
371 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
372 "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1);
373 // If we have both vc80 and vc90, pick version we were compiled with.
374 if (hasSDKDir && windowsSDKInstallDir[0]) {
375 path = windowsSDKInstallDir;
376 return(true);
377 }
378 return(false);
379}
380
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000381void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple) {
Mike Stump43d81762009-10-08 23:29:47 +0000382 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbarc7064682009-11-12 07:28:29 +0000383 llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
384 if (CIncludeDirs != "") {
Rafael Espindolaaadd7a42009-11-13 05:13:58 +0000385 llvm::SmallVector<llvm::StringRef, 5> dirs;
386 CIncludeDirs.split(dirs, ":");
387 for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
388 i != dirs.end();
389 ++i)
Rafael Espindolaf0a2f512009-11-12 05:48:41 +0000390 AddPath(*i, System, false, false, false);
391 return;
392 }
Mike Stump43d81762009-10-08 23:29:47 +0000393 llvm::Triple::OSType os = triple.getOS();
Mike Stump43d81762009-10-08 23:29:47 +0000394 switch (os) {
395 case llvm::Triple::Win32:
396 {
Mike Stump620d57a2009-10-12 20:50:45 +0000397 std::string VSDir;
John Thompson75ee3bd2009-11-21 00:15:52 +0000398 std::string WindowsSDKDir;
Mike Stump620d57a2009-10-12 20:50:45 +0000399 if (getVisualStudioDir(VSDir)) {
400 AddPath(VSDir + "\\VC\\include", System, false, false, false);
John Thompson75ee3bd2009-11-21 00:15:52 +0000401 if (getWindowsSDKDir(WindowsSDKDir))
402 AddPath(WindowsSDKDir, System, false, false, false);
403 else
404 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
405 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000406 }
John Thompson9319f022009-11-23 17:49:27 +0000407 else {
408 // Default install paths.
409 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
410 System, false, false, false);
411 AddPath(
412 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
413 System, false, false, false);
414 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
415 System, false, false, false);
416 AddPath(
417 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
418 System, false, false, false);
419 // For some clang developers.
420 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
421 System, false, false, false);
422 AddPath(
423 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
424 System, false, false, false);
425 }
Mike Stump43d81762009-10-08 23:29:47 +0000426 }
427 break;
Mike Stump43d81762009-10-08 23:29:47 +0000428 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000429 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000430 AddPath("c:/mingw/include", System, true, false, false);
431 break;
432 default:
Mike Stump43d81762009-10-08 23:29:47 +0000433 break;
434 }
John Thompsond3f88342009-10-13 18:51:32 +0000435
436 AddPath("/usr/local/include", System, false, false, false);
437 AddPath("/usr/include", System, false, false, false);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000438}
439
440void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
441 llvm::Triple::OSType os = triple.getOS();
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000442 llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
443 if (CxxIncludeRoot != "") {
444 llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
445 if (CxxIncludeArch == "")
446 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
447 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, triple);
448 else
449 AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
450 CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR, triple);
451 return;
452 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000453 // FIXME: temporary hack: hard-coded paths.
454 switch (os) {
455 case llvm::Triple::Cygwin:
456 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
457 System, true, false, false);
458 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
459 System, true, false, false);
460 break;
461 case llvm::Triple::MinGW64:
462 // Try gcc 4.4.0
463 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
464 // Try gcc 4.3.0
465 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
466 // Fall through.
467 case llvm::Triple::MinGW32:
468 // Try gcc 4.4.0
469 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
470 // Try gcc 4.3.0
471 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
472 break;
473 case llvm::Triple::Darwin:
474 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000475 "i686-apple-darwin10", "", "x86_64", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000476 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000477 "i686-apple-darwin8", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000478 break;
479 case llvm::Triple::Linux:
480 // Ubuntu 7.10 - Gutsy Gibbon
481 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000482 "i486-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000483 // Ubuntu 9.04
484 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000485 "x86_64-linux-gnu","32", "", triple);
Sebastian Redl5114eca2009-11-05 17:44:49 +0000486 // Ubuntu 9.10
487 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000488 "x86_64-linux-gnu", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000489 // Fedora 8
490 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000491 "i386-redhat-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000492 // Fedora 9
493 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000494 "i386-redhat-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000495 // Fedora 10
496 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000497 "i386-redhat-linux","", "", triple);
Nuno Lopes189a1482009-11-17 15:28:35 +0000498
499 // Fedora 11
500 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
501 "i586-redhat-linux","", "", triple);
502
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000503 // openSUSE 11.1 32 bit
504 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000505 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000506 // openSUSE 11.1 64 bit
507 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000508 "x86_64-suse-linux", "32", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000509 // openSUSE 11.2
510 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000511 "i586-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000512 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000513 "x86_64-suse-linux", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000514 // Arch Linux 2008-06-24
515 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000516 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000517 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000518 "x86_64-unknown-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000519 // Gentoo x86 2009.1 stable
520 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000521 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000522 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000523 // Gentoo x86 2009.0 stable
524 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000525 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000526 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000527 // Gentoo x86 2008.0 stable
528 AddGnuCPlusPlusIncludePaths(
John Thompson40d1bb62009-11-05 22:03:02 +0000529 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000530 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000531 // Ubuntu 8.10
532 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000533 "i486-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000534 // Ubuntu 9.04
535 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000536 "i486-linux-gnu","", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000537 // Gentoo amd64 stable
538 AddGnuCPlusPlusIncludePaths(
539 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000540 "i686-pc-linux-gnu", "", "", triple);
Benjamin Kramer5d7a1882009-10-30 12:57:13 +0000541 // Exherbo (2009-10-26)
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000542 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
543 "x86_64-pc-linux-gnu", "32", "", triple);
544 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
545 "i686-pc-linux-gnu", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000546 break;
547 case llvm::Triple::FreeBSD:
548 // DragonFly
549 AddPath("/usr/include/c++/4.1", System, true, false, false);
550 // FreeBSD
551 AddPath("/usr/include/c++/4.2", System, true, false, false);
552 break;
553 case llvm::Triple::Solaris:
554 // Solaris - Fall though..
555 case llvm::Triple::AuroraUX:
556 // AuroraUX
557 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Rafael Espindolaab7ae952009-11-16 19:49:37 +0000558 "i386-pc-solaris2.11", "", "", triple);
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000559 break;
560 default:
561 break;
562 }
563}
564
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000565void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
566 const llvm::Triple &triple) {
Rafael Espindola6ec18a32009-11-23 16:31:19 +0000567 if (Lang.CPlusPlus)
568 AddDefaultCPlusPlusIncludePaths(triple);
569
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000570 AddDefaultCIncludePaths(triple);
Daniel Dunbare1665822009-11-07 04:20:39 +0000571
572 // Add the default framework include paths on Darwin.
573 if (triple.getOS() == llvm::Triple::Darwin) {
574 AddPath("/System/Library/Frameworks", System, true, false, true);
575 AddPath("/Library/Frameworks", System, true, false, true);
576 }
Rafael Espindolae4b255c2009-10-27 14:47:31 +0000577}
578
Nico Weber0fca0222008-08-22 09:25:22 +0000579/// RemoveDuplicates - If there are duplicate directory entries in the specified
580/// search list, remove the later (dead) ones.
581static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
582 bool Verbose) {
583 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
584 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
585 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
586 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000587 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Chris Lattner43eee072009-02-08 01:00:10 +0000589 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattner43eee072009-02-08 01:00:10 +0000591 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000592 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000593 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000594 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000595 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000596 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000597 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000598 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000599 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000600 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000601 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000602 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000603 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000604 }
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Chris Lattner30f05b52009-02-08 00:55:22 +0000606 // If we have a normal #include dir/framework/headermap that is shadowed
607 // later in the chain by a system include location, we actually want to
608 // ignore the user's request and drop the user dir... keeping the system
609 // dir. This is weird, but required to emulate GCC's search path correctly.
610 //
611 // Since dupes of system dirs are rare, just rescan to find the original
612 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000613 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000614 // Find the dir that this is the same of.
615 unsigned FirstDir;
616 for (FirstDir = 0; ; ++FirstDir) {
617 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Chris Lattner43eee072009-02-08 01:00:10 +0000619 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
620
Chris Lattner30f05b52009-02-08 00:55:22 +0000621 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000622 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000623 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Chris Lattner30f05b52009-02-08 00:55:22 +0000625 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000626 if (CurEntry.isNormalDir())
627 isSame = SearchEntry.getDir() == CurEntry.getDir();
628 else if (CurEntry.isFramework())
629 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000630 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000631 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
632 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Chris Lattner30f05b52009-02-08 00:55:22 +0000635 if (isSame)
636 break;
637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Chris Lattner30f05b52009-02-08 00:55:22 +0000639 // If the first dir in the search path is a non-system dir, zap it
640 // instead of the system one.
641 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
642 DirToRemove = FirstDir;
643 }
644
645 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000646 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
647 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000648 if (DirToRemove != i)
649 fprintf(stderr, " as it is a non-system directory that duplicates"
650 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000651 }
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Chris Lattner7a739402008-09-26 17:46:45 +0000653 // This is reached if the current entry is a duplicate. Remove the
654 // DirToRemove (usually the current dir).
655 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000656 --i;
657 }
658}
659
660
661void InitHeaderSearch::Realize() {
662 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
663 std::vector<DirectoryLookup> SearchList;
664 SearchList = IncludeGroup[Angled];
665 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
666 IncludeGroup[System].end());
667 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
668 IncludeGroup[After].end());
669 RemoveDuplicates(SearchList, Verbose);
670 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Nico Weber0fca0222008-08-22 09:25:22 +0000672 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000673 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000674 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Nico Weber0fca0222008-08-22 09:25:22 +0000676
677 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
678 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
679 DontSearchCurDir);
680
681 // If verbose, print the list of directories that will be searched.
682 if (Verbose) {
683 fprintf(stderr, "#include \"...\" search starts here:\n");
684 unsigned QuotedIdx = IncludeGroup[Quoted].size();
685 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
686 if (i == QuotedIdx)
687 fprintf(stderr, "#include <...> search starts here:\n");
688 const char *Name = SearchList[i].getName();
689 const char *Suffix;
690 if (SearchList[i].isNormalDir())
691 Suffix = "";
692 else if (SearchList[i].isFramework())
693 Suffix = " (framework directory)";
694 else {
695 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
696 Suffix = " (headermap)";
697 }
698 fprintf(stderr, " %s%s\n", Name, Suffix);
699 }
700 fprintf(stderr, "End of search list.\n");
701 }
702}
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000703
Daniel Dunbar5814e652009-11-11 21:44:21 +0000704void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
705 const HeaderSearchOptions &HSOpts,
706 const LangOptions &Lang,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000707 const llvm::Triple &Triple) {
708 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
709
710 // Add the user defined entries.
711 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
712 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbar1b483e72009-11-17 05:04:15 +0000713 Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
714 false);
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000715 }
716
717 // Add entries from CPATH and friends.
718 Init.AddDelimitedPaths(HSOpts.EnvIncPath.c_str());
Daniel Dunbarc363cb12009-11-16 22:38:40 +0000719 if (Lang.CPlusPlus && Lang.ObjC1)
720 Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath.c_str());
721 else if (Lang.CPlusPlus)
722 Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath.c_str());
723 else if (Lang.ObjC1)
724 Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath.c_str());
725 else
726 Init.AddDelimitedPaths(HSOpts.CEnvIncPath.c_str());
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000727
728 if (!HSOpts.BuiltinIncludePath.empty()) {
729 // Ignore the sys root, we *always* look for clang headers relative to
730 // supplied path.
Daniel Dunbar2cdafa82009-11-09 23:02:47 +0000731 Init.AddPath(HSOpts.BuiltinIncludePath, System,
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000732 false, false, false, /*IgnoreSysRoot=*/ true);
733 }
734
Daniel Dunbardd35ce92009-11-07 04:58:12 +0000735 if (HSOpts.UseStandardIncludes)
Daniel Dunbar63c8b772009-11-07 04:20:50 +0000736 Init.AddDefaultSystemIncludePaths(Lang, Triple);
737
738 Init.Realize();
739}