blob: 0506975b027f619b36927cb79296927e8f59adcb [file] [log] [blame]
Nick Lewyckye602efc2010-07-25 03:12:58 +00001//===--- InitHeaderSearch.cpp - Initialize header search paths ------------===//
Nico Webered9b4102008-08-22 09:25:22 +00002//
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
Nico Webered9b4102008-08-22 09:25:22 +000014#include "clang/Basic/FileManager.h"
15#include "clang/Basic/LangOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Config/config.h" // C_INCLUDE_DIRS
Mehdi Amini9670f842016-07-18 19:02:11 +000017#include "clang/Frontend/Utils.h"
Chandler Carruthaa36b892015-12-30 03:40:23 +000018#include "clang/Lex/HeaderMap.h"
Daniel Dunbar08d5669b2009-11-07 04:20:50 +000019#include "clang/Lex/HeaderSearch.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Lex/HeaderSearchOptions.h"
Nico Webered9b4102008-08-22 09:25:22 +000021#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "llvm/ADT/SmallString.h"
Rafael Espindola46129b02009-11-13 05:13:58 +000023#include "llvm/ADT/SmallVector.h"
Rafael Espindolaf401fa02009-11-12 05:48:41 +000024#include "llvm/ADT/StringExtras.h"
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000025#include "llvm/ADT/Triple.h"
Benjamin Kramerc6ad84c2009-12-08 12:38:20 +000026#include "llvm/ADT/Twine.h"
Chandler Carruthdf527832011-11-04 23:49:05 +000027#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000028#include "llvm/Support/Path.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000029#include "llvm/Support/raw_ostream.h"
Dylan Noblesmith86780e92012-02-01 14:25:28 +000030
Nico Webered9b4102008-08-22 09:25:22 +000031using namespace clang;
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000032using namespace clang::frontend;
33
34namespace {
35
36/// InitHeaderSearch - This class makes it easier to set the search paths of
37/// a HeaderSearch object. InitHeaderSearch stores several search path lists
38/// internally, which can be sent to a HeaderSearch object in one swoop.
39class InitHeaderSearch {
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +000040 std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath;
41 typedef std::vector<std::pair<IncludeDirGroup,
42 DirectoryLookup> >::const_iterator path_iterator;
Richard Smith8acadcb2012-06-13 20:27:03 +000043 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
Chris Lattner0c64f4b2011-06-16 22:56:45 +000044 HeaderSearch &Headers;
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000045 bool Verbose;
Michael J. Spencerbc7fcc22010-12-25 20:09:27 +000046 std::string IncludeSysroot;
Daniel Dunbar335a37b2013-01-29 23:59:43 +000047 bool HasSysroot;
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000048
49public:
50
Chris Lattner0e62c1c2011-07-23 10:55:15 +000051 InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
Michael J. Spencerbc7fcc22010-12-25 20:09:27 +000052 : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
Daniel Dunbar335a37b2013-01-29 23:59:43 +000053 HasSysroot(!(sysroot.empty() || sysroot == "/")) {
Chandler Carrutheb842002010-11-15 07:15:26 +000054 }
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000055
Daniel Dunbarc6c23752013-01-30 01:06:03 +000056 /// AddPath - Add the specified path to the specified group list, prefixing
57 /// the sysroot if used.
58 void AddPath(const Twine &Path, IncludeDirGroup Group, bool isFramework);
59
60 /// AddUnmappedPath - Add the specified path to the specified group list,
61 /// without performing any sysroot remapping.
62 void AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
63 bool isFramework);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000064
Richard Smith8acadcb2012-06-13 20:27:03 +000065 /// AddSystemHeaderPrefix - Add the specified prefix to the system header
66 /// prefix list.
67 void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
Benjamin Kramer3204b152015-05-29 19:42:19 +000068 SystemHeaderPrefixes.emplace_back(Prefix, IsSystemHeader);
Richard Smith8acadcb2012-06-13 20:27:03 +000069 }
70
mike-m6fa3cd22010-05-05 17:00:31 +000071 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000072 /// libstdc++.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000073 void AddGnuCPlusPlusIncludePaths(StringRef Base,
74 StringRef ArchDir,
75 StringRef Dir32,
76 StringRef Dir64,
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000077 const llvm::Triple &triple);
78
Michael J. Spencerbc721822010-12-21 16:45:42 +000079 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000080 /// libstdc++.
Aaron Ballmaneceaddc2012-03-25 22:46:17 +000081 void AddMinGWCPlusPlusIncludePaths(StringRef Base,
82 StringRef Arch,
83 StringRef Version);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000084
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000085 // AddDefaultCIncludePaths - Add paths that should always be searched.
mike-mc6da2612010-05-16 19:03:52 +000086 void AddDefaultCIncludePaths(const llvm::Triple &triple,
87 const HeaderSearchOptions &HSOpts);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000088
89 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
90 // compiling c++.
Douglas Gregor7a200962011-06-27 15:47:15 +000091 void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple,
92 const HeaderSearchOptions &HSOpts);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000093
94 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
95 /// that e.g. stdio.h is found.
Daniel Dunbarb25bfde2011-10-11 18:20:10 +000096 void AddDefaultIncludePaths(const LangOptions &Lang,
97 const llvm::Triple &triple,
98 const HeaderSearchOptions &HSOpts);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +000099
100 /// Realize - Merges all search path lists into one list and send it to
101 /// HeaderSearch.
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000102 void Realize(const LangOptions &Lang);
Daniel Dunbar4df9aa22009-11-09 23:02:47 +0000103};
104
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000105} // end anonymous namespace.
Nico Webered9b4102008-08-22 09:25:22 +0000106
Daniel Dunbar2e8eb012013-01-29 23:59:37 +0000107static bool CanPrefixSysroot(StringRef Path) {
Hans Wennborg501eadb2014-03-12 16:07:46 +0000108#if defined(LLVM_ON_WIN32)
Daniel Dunbar2e8eb012013-01-29 23:59:37 +0000109 return !Path.empty() && llvm::sys::path::is_separator(Path[0]);
110#else
111 return llvm::sys::path::is_absolute(Path);
112#endif
113}
114
Daniel Dunbar9f237452013-01-30 00:19:24 +0000115void InitHeaderSearch::AddPath(const Twine &Path, IncludeDirGroup Group,
Daniel Dunbarc6c23752013-01-30 01:06:03 +0000116 bool isFramework) {
117 // Add the path with sysroot prepended, if desired and this is a system header
118 // group.
119 if (HasSysroot) {
120 SmallString<256> MappedPathStorage;
121 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
122 if (CanPrefixSysroot(MappedPathStr)) {
123 AddUnmappedPath(IncludeSysroot + Path, Group, isFramework);
124 return;
125 }
126 }
Mike Stump11289f42009-09-09 15:08:12 +0000127
Daniel Dunbarc6c23752013-01-30 01:06:03 +0000128 AddUnmappedPath(Path, Group, isFramework);
129}
130
131void InitHeaderSearch::AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
132 bool isFramework) {
133 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
134
135 FileManager &FM = Headers.getFileMgr();
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000136 SmallString<256> MappedPathStorage;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000137 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
Mike Stump11289f42009-09-09 15:08:12 +0000138
Nico Webered9b4102008-08-22 09:25:22 +0000139 // Compute the DirectoryLookup type.
Chris Lattner66a740e2008-10-27 01:19:25 +0000140 SrcMgr::CharacteristicKind Type;
Daniel Dunbar9f237452013-01-30 00:19:24 +0000141 if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) {
Chris Lattnerb03dc762008-09-26 21:18:42 +0000142 Type = SrcMgr::C_User;
Daniel Dunbar9f237452013-01-30 00:19:24 +0000143 } else if (Group == ExternCSystem) {
Chris Lattnerb03dc762008-09-26 21:18:42 +0000144 Type = SrcMgr::C_ExternCSystem;
Daniel Dunbar9f237452013-01-30 00:19:24 +0000145 } else {
146 Type = SrcMgr::C_System;
147 }
Mike Stump11289f42009-09-09 15:08:12 +0000148
Nico Webered9b4102008-08-22 09:25:22 +0000149 // If the directory exists, add it.
Chris Lattner5159f612010-11-23 08:35:12 +0000150 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
Daniel Dunbarae4feb62013-01-25 01:50:28 +0000151 IncludePath.push_back(
152 std::make_pair(Group, DirectoryLookup(DE, Type, isFramework)));
Nico Webered9b4102008-08-22 09:25:22 +0000153 return;
154 }
Mike Stump11289f42009-09-09 15:08:12 +0000155
Nico Webered9b4102008-08-22 09:25:22 +0000156 // Check to see if this is an apple-style headermap (which are not allowed to
157 // be frameworks).
158 if (!isFramework) {
Chris Lattner5159f612010-11-23 08:35:12 +0000159 if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
Nico Webered9b4102008-08-22 09:25:22 +0000160 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
161 // It is a headermap, add it to the search path.
Daniel Dunbarae4feb62013-01-25 01:50:28 +0000162 IncludePath.push_back(
163 std::make_pair(Group,
164 DirectoryLookup(HM, Type, Group == IndexHeaderMap)));
Nico Webered9b4102008-08-22 09:25:22 +0000165 return;
166 }
167 }
168 }
Mike Stump11289f42009-09-09 15:08:12 +0000169
Nico Webered9b4102008-08-22 09:25:22 +0000170 if (Verbose)
Chandler Carruth03ac1b02010-11-15 00:48:13 +0000171 llvm::errs() << "ignoring nonexistent directory \""
172 << MappedPathStr << "\"\n";
Nico Webered9b4102008-08-22 09:25:22 +0000173}
174
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000175void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base,
176 StringRef ArchDir,
177 StringRef Dir32,
178 StringRef Dir64,
Rafael Espindola0a1ac332009-10-14 17:09:44 +0000179 const llvm::Triple &triple) {
Rafael Espindola962e5182009-11-23 16:31:19 +0000180 // Add the base dir
Daniel Dunbar9f237452013-01-30 00:19:24 +0000181 AddPath(Base, CXXSystem, false);
Rafael Espindolaabab8792009-11-16 19:49:37 +0000182
183 // Add the multilib dirs
Rafael Espindola0a1ac332009-10-14 17:09:44 +0000184 llvm::Triple::ArchType arch = triple.getArch();
185 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
Rafael Espindola0a1ac332009-10-14 17:09:44 +0000186 if (is64bit)
Daniel Dunbar9f237452013-01-30 00:19:24 +0000187 AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, false);
Rafael Espindola0a1ac332009-10-14 17:09:44 +0000188 else
Daniel Dunbar9f237452013-01-30 00:19:24 +0000189 AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, false);
Rafael Espindola962e5182009-11-23 16:31:19 +0000190
191 // Add the backward dir
Daniel Dunbar9f237452013-01-30 00:19:24 +0000192 AddPath(Base + "/backward", CXXSystem, false);
Rafael Espindolac3031a92009-10-06 01:33:02 +0000193}
Nico Webered9b4102008-08-22 09:25:22 +0000194
Aaron Ballman9345d682012-03-25 15:47:41 +0000195void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base,
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000196 StringRef Arch,
197 StringRef Version) {
198 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
Daniel Dunbar9f237452013-01-30 00:19:24 +0000199 CXXSystem, false);
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000200 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
Daniel Dunbar9f237452013-01-30 00:19:24 +0000201 CXXSystem, false);
Aaron Ballmaneceaddc2012-03-25 22:46:17 +0000202 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
Daniel Dunbar9f237452013-01-30 00:19:24 +0000203 CXXSystem, false);
Aaron Ballman9345d682012-03-25 15:47:41 +0000204}
205
mike-mc6da2612010-05-16 19:03:52 +0000206void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
207 const HeaderSearchOptions &HSOpts) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000208 llvm::Triple::OSType os = triple.getOS();
209
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000210 if (HSOpts.UseStandardSystemIncludes) {
211 switch (os) {
Ed Schouten2b60d1e2015-03-11 08:46:01 +0000212 case llvm::Triple::CloudABI:
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000213 case llvm::Triple::FreeBSD:
214 case llvm::Triple::NetBSD:
Hans Wennborgae298f22012-08-02 12:27:08 +0000215 case llvm::Triple::OpenBSD:
Derek Schuff6ab52fa2015-03-30 20:31:33 +0000216 case llvm::Triple::NaCl:
Filipe Cabecinhasc888e192015-10-14 12:25:43 +0000217 case llvm::Triple::PS4:
Andrey Bokhanko0b135e02015-12-16 13:27:38 +0000218 case llvm::Triple::ELFIAMCU:
Petr Hosek1aea1e72018-03-02 07:19:42 +0000219 case llvm::Triple::Fuchsia:
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000220 break;
Yaron Keren1c0070c2015-07-02 04:45:27 +0000221 case llvm::Triple::Win32:
222 if (triple.getEnvironment() != llvm::Triple::Cygnus)
223 break;
Galina Kistanovae37ad5a2017-06-03 06:27:16 +0000224 LLVM_FALLTHROUGH;
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000225 default:
226 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbar9f237452013-01-30 00:19:24 +0000227 AddPath("/usr/local/include", System, false);
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000228 break;
229 }
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000230 }
mike-mc6da2612010-05-16 19:03:52 +0000231
232 // Builtin includes use #include_next directives and should be positioned
233 // just prior C include dirs.
234 if (HSOpts.UseBuiltinIncludes) {
235 // Ignore the sys root, we *always* look for clang headers relative to
236 // supplied path.
Benjamin Kramer33d43302013-06-13 14:26:04 +0000237 SmallString<128> P = StringRef(HSOpts.ResourceDir);
238 llvm::sys::path::append(P, "include");
Yaron Keren92e1b622015-03-18 10:17:07 +0000239 AddUnmappedPath(P, ExternCSystem, false);
mike-mc6da2612010-05-16 19:03:52 +0000240 }
241
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000242 // All remaining additions are for system include directories, early exit if
243 // we aren't using them.
244 if (!HSOpts.UseStandardSystemIncludes)
245 return;
246
mike-mc6da2612010-05-16 19:03:52 +0000247 // Add dirs specified via 'configure --with-c-include-dirs'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000248 StringRef CIncludeDirs(C_INCLUDE_DIRS);
Daniel Dunbar71ed08b2009-11-12 07:28:29 +0000249 if (CIncludeDirs != "") {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000250 SmallVector<StringRef, 5> dirs;
Rafael Espindola46129b02009-11-13 05:13:58 +0000251 CIncludeDirs.split(dirs, ":");
Yaron Keren54bb2152015-10-01 11:19:28 +0000252 for (StringRef dir : dirs)
253 AddPath(dir, ExternCSystem, false);
Rafael Espindolaf401fa02009-11-12 05:48:41 +0000254 return;
255 }
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +0000256
Mike Stump904ad902009-10-08 23:29:47 +0000257 switch (os) {
Chandler Carrutha796f532011-11-05 20:17:13 +0000258 case llvm::Triple::Linux:
Fedor Sergeevfaa0a822018-01-23 12:23:52 +0000259 case llvm::Triple::Solaris:
Chandler Carrutha796f532011-11-05 20:17:13 +0000260 llvm_unreachable("Include management is handled in the driver.");
Chandler Carruthdf527832011-11-04 23:49:05 +0000261
Ed Schouten2b60d1e2015-03-11 08:46:01 +0000262 case llvm::Triple::CloudABI: {
263 // <sysroot>/<triple>/include
264 SmallString<128> P = StringRef(HSOpts.ResourceDir);
265 llvm::sys::path::append(P, "../../..", triple.str(), "include");
Yaron Keren92e1b622015-03-18 10:17:07 +0000266 AddPath(P, System, false);
Ed Schouten2b60d1e2015-03-11 08:46:01 +0000267 break;
268 }
269
Chris Lattnerb986aba2010-04-11 19:29:39 +0000270 case llvm::Triple::Haiku:
Reid Kleckner330fb172016-05-11 16:19:05 +0000271 AddPath("/boot/system/non-packaged/develop/headers", System, false);
272 AddPath("/boot/system/develop/headers/os", System, false);
273 AddPath("/boot/system/develop/headers/os/app", System, false);
274 AddPath("/boot/system/develop/headers/os/arch", System, false);
275 AddPath("/boot/system/develop/headers/os/device", System, false);
276 AddPath("/boot/system/develop/headers/os/drivers", System, false);
277 AddPath("/boot/system/develop/headers/os/game", System, false);
278 AddPath("/boot/system/develop/headers/os/interface", System, false);
279 AddPath("/boot/system/develop/headers/os/kernel", System, false);
280 AddPath("/boot/system/develop/headers/os/locale", System, false);
281 AddPath("/boot/system/develop/headers/os/mail", System, false);
282 AddPath("/boot/system/develop/headers/os/media", System, false);
283 AddPath("/boot/system/develop/headers/os/midi", System, false);
284 AddPath("/boot/system/develop/headers/os/midi2", System, false);
285 AddPath("/boot/system/develop/headers/os/net", System, false);
286 AddPath("/boot/system/develop/headers/os/opengl", System, false);
287 AddPath("/boot/system/develop/headers/os/storage", System, false);
288 AddPath("/boot/system/develop/headers/os/support", System, false);
289 AddPath("/boot/system/develop/headers/os/translation", System, false);
290 AddPath("/boot/system/develop/headers/os/add-ons/graphics", System, false);
291 AddPath("/boot/system/develop/headers/os/add-ons/input_server", System, false);
292 AddPath("/boot/system/develop/headers/os/add-ons/mail_daemon", System, false);
293 AddPath("/boot/system/develop/headers/os/add-ons/registrar", System, false);
294 AddPath("/boot/system/develop/headers/os/add-ons/screen_saver", System, false);
295 AddPath("/boot/system/develop/headers/os/add-ons/tracker", System, false);
296 AddPath("/boot/system/develop/headers/os/be_apps/Deskbar", System, false);
297 AddPath("/boot/system/develop/headers/os/be_apps/NetPositive", System, false);
298 AddPath("/boot/system/develop/headers/os/be_apps/Tracker", System, false);
299 AddPath("/boot/system/develop/headers/3rdparty", System, false);
300 AddPath("/boot/system/develop/headers/bsd", System, false);
301 AddPath("/boot/system/develop/headers/glibc", System, false);
302 AddPath("/boot/system/develop/headers/posix", System, false);
303 AddPath("/boot/system/develop/headers", System, false);
Eli Friedman04831922010-08-22 01:00:03 +0000304 break;
Douglas Gregor9fabd852011-07-01 22:41:14 +0000305 case llvm::Triple::RTEMS:
306 break;
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000307 case llvm::Triple::Win32:
308 switch (triple.getEnvironment()) {
309 default: llvm_unreachable("Include management is handled in the driver.");
310 case llvm::Triple::Cygnus:
311 AddPath("/usr/include/w32api", System, false);
312 break;
313 case llvm::Triple::GNU:
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000314 break;
Douglas Gregor7a200962011-06-27 15:47:15 +0000315 }
Mike Stump904ad902009-10-08 23:29:47 +0000316 break;
317 default:
Mike Stump904ad902009-10-08 23:29:47 +0000318 break;
319 }
John Thompson4334ce62009-10-13 18:51:32 +0000320
Ed Schouten2b60d1e2015-03-11 08:46:01 +0000321 switch (os) {
322 case llvm::Triple::CloudABI:
323 case llvm::Triple::RTEMS:
Derek Schuff6ab52fa2015-03-30 20:31:33 +0000324 case llvm::Triple::NaCl:
Andrey Bokhanko0b135e02015-12-16 13:27:38 +0000325 case llvm::Triple::ELFIAMCU:
Petr Hosek1aea1e72018-03-02 07:19:42 +0000326 case llvm::Triple::Fuchsia:
Ed Schouten2b60d1e2015-03-11 08:46:01 +0000327 break;
Filipe Cabecinhasc888e192015-10-14 12:25:43 +0000328 case llvm::Triple::PS4: {
329 // <isysroot> gets prepended later in AddPath().
330 std::string BaseSDKPath = "";
331 if (!HasSysroot) {
Paul Robinson9d613612016-05-16 17:22:25 +0000332 const char *envValue = getenv("SCE_ORBIS_SDK_DIR");
Filipe Cabecinhasc888e192015-10-14 12:25:43 +0000333 if (envValue)
334 BaseSDKPath = envValue;
335 else {
336 // HSOpts.ResourceDir variable contains the location of Clang's
337 // resource files.
338 // Assuming that Clang is configured for PS4 without
339 // --with-clang-resource-dir option, the location of Clang's resource
340 // files is <SDK_DIR>/host_tools/lib/clang
341 SmallString<128> P = StringRef(HSOpts.ResourceDir);
342 llvm::sys::path::append(P, "../../..");
343 BaseSDKPath = P.str();
344 }
345 }
346 AddPath(BaseSDKPath + "/target/include", System, false);
347 if (triple.isPS4CPU())
348 AddPath(BaseSDKPath + "/target/include_common", System, false);
Galina Kistanovae37ad5a2017-06-03 06:27:16 +0000349 LLVM_FALLTHROUGH;
Filipe Cabecinhasc888e192015-10-14 12:25:43 +0000350 }
Ed Schouten2b60d1e2015-03-11 08:46:01 +0000351 default:
Daniel Dunbar9f237452013-01-30 00:19:24 +0000352 AddPath("/usr/include", ExternCSystem, false);
Ed Schouten2b60d1e2015-03-11 08:46:01 +0000353 break;
354 }
Rafael Espindola177f1d92009-10-27 14:47:31 +0000355}
356
Chris Lattner5561bf32010-05-05 05:28:39 +0000357void InitHeaderSearch::
Douglas Gregor7a200962011-06-27 15:47:15 +0000358AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) {
Rafael Espindola177f1d92009-10-27 14:47:31 +0000359 llvm::Triple::OSType os = triple.getOS();
360 // FIXME: temporary hack: hard-coded paths.
Daniel Dunbar14ad22f2011-04-19 21:43:27 +0000361
362 if (triple.isOSDarwin()) {
Daniel Dunbareaff5fa2010-05-28 01:54:31 +0000363 switch (triple.getArch()) {
364 default: break;
365
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000366 case llvm::Triple::ppc:
Douglas Gregoreb0bdf02010-05-29 01:15:12 +0000367 case llvm::Triple::ppc64:
Douglas Gregor117ef272010-05-29 01:21:11 +0000368 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000369 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregor117ef272010-05-29 01:21:11 +0000370 triple);
Douglas Gregoreb0bdf02010-05-29 01:15:12 +0000371 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +0000372 "powerpc-apple-darwin10", "", "ppc64",
Douglas Gregoreb0bdf02010-05-29 01:15:12 +0000373 triple);
374 break;
375
Daniel Dunbareaff5fa2010-05-28 01:54:31 +0000376 case llvm::Triple::x86:
377 case llvm::Triple::x86_64:
378 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
379 "i686-apple-darwin10", "", "x86_64", triple);
380 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
381 "i686-apple-darwin8", "", "", triple);
382 break;
383
384 case llvm::Triple::arm:
385 case llvm::Triple::thumb:
386 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
387 "arm-apple-darwin10", "v7", "", triple);
388 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
389 "arm-apple-darwin10", "v6", "", triple);
390 break;
Tim Northovera2ee4332014-03-29 15:09:45 +0000391
Tim Northover573cbee2014-05-24 12:52:07 +0000392 case llvm::Triple::aarch64:
Tim Northovera2ee4332014-03-29 15:09:45 +0000393 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
394 "arm64-apple-darwin10", "", "", triple);
395 break;
Daniel Dunbareaff5fa2010-05-28 01:54:31 +0000396 }
Daniel Dunbar14ad22f2011-04-19 21:43:27 +0000397 return;
398 }
399
400 switch (os) {
Chandler Carrutha796f532011-11-05 20:17:13 +0000401 case llvm::Triple::Linux:
Fedor Sergeevfaa0a822018-01-23 12:23:52 +0000402 case llvm::Triple::Solaris:
Chandler Carrutha796f532011-11-05 20:17:13 +0000403 llvm_unreachable("Include management is handled in the driver.");
Sylvestre Ledru75c29142014-08-18 15:13:44 +0000404 break;
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000405 case llvm::Triple::Win32:
406 switch (triple.getEnvironment()) {
407 default: llvm_unreachable("Include management is handled in the driver.");
408 case llvm::Triple::Cygnus:
409 // Cygwin-1.7
410 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.7.3");
411 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3");
412 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
413 // g++-4 / Cygwin-1.5
414 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
415 break;
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000416 }
Yaron Keren1c0070c2015-07-02 04:45:27 +0000417 break;
Chris Lattner002ba6b2010-01-09 05:41:14 +0000418 case llvm::Triple::DragonFly:
Dimitry Andricf59a2b32015-12-27 10:01:44 +0000419 AddPath("/usr/include/c++/5.0", CXXSystem, false);
Chris Lattner002ba6b2010-01-09 05:41:14 +0000420 break;
Daniel Dunbarea3813f2010-08-01 23:13:54 +0000421 case llvm::Triple::OpenBSD: {
422 std::string t = triple.getTriple();
423 if (t.substr(0, 6) == "x86_64")
424 t.replace(0, 6, "amd64");
425 AddGnuCPlusPlusIncludePaths("/usr/include/g++",
426 t, "", "", triple);
427 break;
428 }
Chris Lattner3e2ee142010-07-07 16:01:42 +0000429 case llvm::Triple::Minix:
430 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
431 "", "", "", triple);
432 break;
Rafael Espindola177f1d92009-10-27 14:47:31 +0000433 default:
434 break;
435 }
436}
437
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000438void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang,
439 const llvm::Triple &triple,
mike-mc6da2612010-05-16 19:03:52 +0000440 const HeaderSearchOptions &HSOpts) {
Chandler Carruthdf527832011-11-04 23:49:05 +0000441 // NB: This code path is going away. All of the logic is moving into the
442 // driver which has the information necessary to do target-specific
443 // selections of default include paths. Each target which moves there will be
444 // exempted from this logic here until we can delete the entire pile of code.
445 switch (triple.getOS()) {
446 default:
447 break; // Everything else continues to use this routine's logic.
448
Chandler Carrutha796f532011-11-05 20:17:13 +0000449 case llvm::Triple::Linux:
Fedor Sergeevfaa0a822018-01-23 12:23:52 +0000450 case llvm::Triple::Solaris:
Chandler Carruthdf527832011-11-04 23:49:05 +0000451 return;
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000452
453 case llvm::Triple::Win32:
Yaron Keren1c0070c2015-07-02 04:45:27 +0000454 if (triple.getEnvironment() != llvm::Triple::Cygnus ||
Eric Christopher610952e2014-12-05 00:22:48 +0000455 triple.isOSBinFormatMachO())
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000456 return;
457 break;
Chandler Carruthdf527832011-11-04 23:49:05 +0000458 }
459
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000460 if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes &&
461 HSOpts.UseStandardSystemIncludes) {
Douglas Gregorf4016fd2011-07-29 20:21:18 +0000462 if (HSOpts.UseLibcxx) {
463 if (triple.isOSDarwin()) {
464 // On Darwin, libc++ may be installed alongside the compiler in
Justin Bogner13a641b2013-11-15 18:07:59 +0000465 // include/c++/v1.
Benjamin Kramer33d43302013-06-13 14:26:04 +0000466 if (!HSOpts.ResourceDir.empty()) {
467 // Remove version from foo/lib/clang/version
468 StringRef NoVer = llvm::sys::path::parent_path(HSOpts.ResourceDir);
469 // Remove clang from foo/lib/clang
Justin Bogner13a641b2013-11-15 18:07:59 +0000470 StringRef Lib = llvm::sys::path::parent_path(NoVer);
471 // Remove lib from foo/lib
472 SmallString<128> P = llvm::sys::path::parent_path(Lib);
473
474 // Get foo/include/c++/v1
475 llvm::sys::path::append(P, "include", "c++", "v1");
Yaron Keren92e1b622015-03-18 10:17:07 +0000476 AddUnmappedPath(P, CXXSystem, false);
Douglas Gregorf4016fd2011-07-29 20:21:18 +0000477 }
478 }
Daniel Dunbar9f237452013-01-30 00:19:24 +0000479 AddPath("/usr/include/c++/v1", CXXSystem, false);
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000480 } else {
Douglas Gregor7a200962011-06-27 15:47:15 +0000481 AddDefaultCPlusPlusIncludePaths(triple, HSOpts);
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000482 }
Bob Wilsonb02ea3d2011-06-21 21:12:29 +0000483 }
Rafael Espindola962e5182009-11-23 16:31:19 +0000484
mike-mc6da2612010-05-16 19:03:52 +0000485 AddDefaultCIncludePaths(triple, HSOpts);
Daniel Dunbarec879912009-11-07 04:20:39 +0000486
487 // Add the default framework include paths on Darwin.
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000488 if (HSOpts.UseStandardSystemIncludes) {
489 if (triple.isOSDarwin()) {
Daniel Dunbar9f237452013-01-30 00:19:24 +0000490 AddPath("/System/Library/Frameworks", System, true);
491 AddPath("/Library/Frameworks", System, true);
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000492 }
Daniel Dunbarec879912009-11-07 04:20:39 +0000493 }
Rafael Espindola177f1d92009-10-27 14:47:31 +0000494}
495
Nico Webered9b4102008-08-22 09:25:22 +0000496/// RemoveDuplicates - If there are duplicate directory entries in the specified
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000497/// search list, remove the later (dead) ones. Returns the number of non-system
498/// headers removed, which is used to update NumAngled.
499static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
500 unsigned First, bool Verbose) {
Nico Webered9b4102008-08-22 09:25:22 +0000501 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
502 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
503 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000504 unsigned NonSystemRemoved = 0;
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000505 for (unsigned i = First; i != SearchList.size(); ++i) {
Chris Lattnere744d322008-09-26 17:46:45 +0000506 unsigned DirToRemove = i;
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattner24c911e2009-02-08 01:00:10 +0000508 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump11289f42009-09-09 15:08:12 +0000509
Chris Lattner24c911e2009-02-08 01:00:10 +0000510 if (CurEntry.isNormalDir()) {
Nico Webered9b4102008-08-22 09:25:22 +0000511 // If this isn't the first time we've seen this dir, remove it.
David Blaikie82e95a32014-11-19 07:49:47 +0000512 if (SeenDirs.insert(CurEntry.getDir()).second)
Nico Webered9b4102008-08-22 09:25:22 +0000513 continue;
Chris Lattner24c911e2009-02-08 01:00:10 +0000514 } else if (CurEntry.isFramework()) {
Nico Webered9b4102008-08-22 09:25:22 +0000515 // If this isn't the first time we've seen this framework dir, remove it.
David Blaikie82e95a32014-11-19 07:49:47 +0000516 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()).second)
Nico Webered9b4102008-08-22 09:25:22 +0000517 continue;
Nico Webered9b4102008-08-22 09:25:22 +0000518 } else {
Chris Lattner24c911e2009-02-08 01:00:10 +0000519 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Webered9b4102008-08-22 09:25:22 +0000520 // If this isn't the first time we've seen this headermap, remove it.
David Blaikie82e95a32014-11-19 07:49:47 +0000521 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()).second)
Nico Webered9b4102008-08-22 09:25:22 +0000522 continue;
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000523 }
Mike Stump11289f42009-09-09 15:08:12 +0000524
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000525 // If we have a normal #include dir/framework/headermap that is shadowed
526 // later in the chain by a system include location, we actually want to
527 // ignore the user's request and drop the user dir... keeping the system
528 // dir. This is weird, but required to emulate GCC's search path correctly.
529 //
530 // Since dupes of system dirs are rare, just rescan to find the original
531 // that we're nuking instead of using a DenseMap.
Chris Lattner24c911e2009-02-08 01:00:10 +0000532 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000533 // Find the dir that this is the same of.
534 unsigned FirstDir;
Alex Lorenz8c1b5c92016-12-02 09:51:51 +0000535 for (FirstDir = First;; ++FirstDir) {
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000536 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump11289f42009-09-09 15:08:12 +0000537
Chris Lattner24c911e2009-02-08 01:00:10 +0000538 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
539
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000540 // If these are different lookup types, then they can't be the dupe.
Chris Lattner24c911e2009-02-08 01:00:10 +0000541 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000542 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000543
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000544 bool isSame;
Chris Lattner24c911e2009-02-08 01:00:10 +0000545 if (CurEntry.isNormalDir())
546 isSame = SearchEntry.getDir() == CurEntry.getDir();
547 else if (CurEntry.isFramework())
548 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000549 else {
Chris Lattner24c911e2009-02-08 01:00:10 +0000550 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
551 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000552 }
Mike Stump11289f42009-09-09 15:08:12 +0000553
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000554 if (isSame)
555 break;
556 }
Mike Stump11289f42009-09-09 15:08:12 +0000557
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000558 // If the first dir in the search path is a non-system dir, zap it
559 // instead of the system one.
560 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
561 DirToRemove = FirstDir;
562 }
563
564 if (Verbose) {
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000565 llvm::errs() << "ignoring duplicate directory \""
566 << CurEntry.getName() << "\"\n";
Chris Lattnerbf20a9a2009-02-08 00:55:22 +0000567 if (DirToRemove != i)
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000568 llvm::errs() << " as it is a non-system directory that duplicates "
569 << "a system directory\n";
Nico Webered9b4102008-08-22 09:25:22 +0000570 }
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000571 if (DirToRemove != i)
572 ++NonSystemRemoved;
Mike Stump11289f42009-09-09 15:08:12 +0000573
Chris Lattnere744d322008-09-26 17:46:45 +0000574 // This is reached if the current entry is a duplicate. Remove the
575 // DirToRemove (usually the current dir).
576 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Webered9b4102008-08-22 09:25:22 +0000577 --i;
578 }
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000579 return NonSystemRemoved;
Nico Webered9b4102008-08-22 09:25:22 +0000580}
581
582
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000583void InitHeaderSearch::Realize(const LangOptions &Lang) {
Nico Webered9b4102008-08-22 09:25:22 +0000584 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
585 std::vector<DirectoryLookup> SearchList;
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000586 SearchList.reserve(IncludePath.size());
Mike Stump11289f42009-09-09 15:08:12 +0000587
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000588 // Quoted arguments go first.
Yaron Keren54bb2152015-10-01 11:19:28 +0000589 for (auto &Include : IncludePath)
590 if (Include.first == Quoted)
591 SearchList.push_back(Include.second);
592
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000593 // Deduplicate and remember index.
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000594 RemoveDuplicates(SearchList, 0, Verbose);
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000595 unsigned NumQuoted = SearchList.size();
Mike Stump11289f42009-09-09 15:08:12 +0000596
Yaron Keren54bb2152015-10-01 11:19:28 +0000597 for (auto &Include : IncludePath)
598 if (Include.first == Angled || Include.first == IndexHeaderMap)
599 SearchList.push_back(Include.second);
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000600
601 RemoveDuplicates(SearchList, NumQuoted, Verbose);
602 unsigned NumAngled = SearchList.size();
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000603
Yaron Keren54bb2152015-10-01 11:19:28 +0000604 for (auto &Include : IncludePath)
605 if (Include.first == System || Include.first == ExternCSystem ||
606 (!Lang.ObjC1 && !Lang.CPlusPlus && Include.first == CSystem) ||
607 (/*FIXME !Lang.ObjC1 && */ Lang.CPlusPlus &&
608 Include.first == CXXSystem) ||
609 (Lang.ObjC1 && !Lang.CPlusPlus && Include.first == ObjCSystem) ||
610 (Lang.ObjC1 && Lang.CPlusPlus && Include.first == ObjCXXSystem))
611 SearchList.push_back(Include.second);
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000612
Yaron Keren54bb2152015-10-01 11:19:28 +0000613 for (auto &Include : IncludePath)
614 if (Include.first == After)
615 SearchList.push_back(Include.second);
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000616
Chris Lattner705c5c82011-06-16 22:58:10 +0000617 // Remove duplicates across both the Angled and System directories. GCC does
618 // this and failing to remove duplicates across these two groups breaks
619 // #include_next.
Chad Rosierfd3c90c2011-10-10 18:44:24 +0000620 unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose);
621 NumAngled -= NonSystemRemoved;
Nico Webered9b4102008-08-22 09:25:22 +0000622
623 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000624 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
Nico Webered9b4102008-08-22 09:25:22 +0000625
Richard Smith8acadcb2012-06-13 20:27:03 +0000626 Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes);
627
Nico Webered9b4102008-08-22 09:25:22 +0000628 // If verbose, print the list of directories that will be searched.
629 if (Verbose) {
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000630 llvm::errs() << "#include \"...\" search starts here:\n";
Nico Webered9b4102008-08-22 09:25:22 +0000631 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
Chris Lattner0c64f4b2011-06-16 22:56:45 +0000632 if (i == NumQuoted)
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000633 llvm::errs() << "#include <...> search starts here:\n";
Mehdi Amini99d1b292016-10-01 16:38:28 +0000634 StringRef Name = SearchList[i].getName();
Nico Webered9b4102008-08-22 09:25:22 +0000635 const char *Suffix;
636 if (SearchList[i].isNormalDir())
637 Suffix = "";
638 else if (SearchList[i].isFramework())
639 Suffix = " (framework directory)";
640 else {
641 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
642 Suffix = " (headermap)";
643 }
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000644 llvm::errs() << " " << Name << Suffix << "\n";
Nico Webered9b4102008-08-22 09:25:22 +0000645 }
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000646 llvm::errs() << "End of search list.\n";
Nico Webered9b4102008-08-22 09:25:22 +0000647 }
648}
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000649
Daniel Dunbar0c6c9302009-11-11 21:44:21 +0000650void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
651 const HeaderSearchOptions &HSOpts,
652 const LangOptions &Lang,
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000653 const llvm::Triple &Triple) {
654 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
655
656 // Add the user defined entries.
657 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
658 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
Daniel Dunbarc6c23752013-01-30 01:06:03 +0000659 if (E.IgnoreSysRoot) {
660 Init.AddUnmappedPath(E.Path, E.Group, E.IsFramework);
661 } else {
662 Init.AddPath(E.Path, E.Group, E.IsFramework);
663 }
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000664 }
665
Daniel Dunbarb25bfde2011-10-11 18:20:10 +0000666 Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000667
Richard Smith8acadcb2012-06-13 20:27:03 +0000668 for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i)
669 Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix,
670 HSOpts.SystemHeaderPrefixes[i].IsSystemHeader);
671
Douglas Gregor3ec66632012-02-02 18:42:48 +0000672 if (HSOpts.UseBuiltinIncludes) {
673 // Set up the builtin include directory in the module map.
Benjamin Kramer33d43302013-06-13 14:26:04 +0000674 SmallString<128> P = StringRef(HSOpts.ResourceDir);
675 llvm::sys::path::append(P, "include");
Yaron Keren92e1b622015-03-18 10:17:07 +0000676 if (const DirectoryEntry *Dir = HS.getFileMgr().getDirectory(P))
Douglas Gregor3ec66632012-02-02 18:42:48 +0000677 HS.getModuleMap().setBuiltinIncludeDir(Dir);
678 }
679
Joerg Sonnenbergercc9c8eb2011-02-22 00:40:56 +0000680 Init.Realize(Lang);
Daniel Dunbar08d5669b2009-11-07 04:20:50 +0000681}