InitHeaderSearch.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. //===--- 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. #include "clang/Frontend/Utils.h"
  14. #include "clang/Basic/FileManager.h"
  15. #include "clang/Basic/LangOptions.h"
  16. #include "clang/Frontend/HeaderSearchOptions.h"
  17. #include "clang/Lex/HeaderSearch.h"
  18. #include "llvm/ADT/SmallString.h"
  19. #include "llvm/ADT/SmallPtrSet.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/StringExtras.h"
  22. #include "llvm/ADT/Triple.h"
  23. #include "llvm/ADT/Twine.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include "llvm/System/Path.h"
  26. #include "llvm/Config/config.h"
  27. #ifdef _MSC_VER
  28. #define WIN32_LEAN_AND_MEAN 1
  29. #include <windows.h>
  30. #endif
  31. using namespace clang;
  32. using namespace clang::frontend;
  33. namespace {
  34. /// InitHeaderSearch - This class makes it easier to set the search paths of
  35. /// a HeaderSearch object. InitHeaderSearch stores several search path lists
  36. /// internally, which can be sent to a HeaderSearch object in one swoop.
  37. class InitHeaderSearch {
  38. std::vector<DirectoryLookup> IncludeGroup[4];
  39. HeaderSearch& Headers;
  40. bool Verbose;
  41. llvm::sys::Path IncludeSysroot;
  42. public:
  43. InitHeaderSearch(HeaderSearch &HS, bool verbose, llvm::StringRef sysroot)
  44. : Headers(HS), Verbose(verbose),
  45. IncludeSysroot((sysroot.empty() || sysroot == "/") ?
  46. llvm::sys::Path::GetRootDirectory() :
  47. llvm::sys::Path(sysroot)) {
  48. }
  49. /// AddPath - Add the specified path to the specified group list.
  50. void AddPath(const llvm::Twine &Path, IncludeDirGroup Group,
  51. bool isCXXAware, bool isUserSupplied,
  52. bool isFramework, bool IgnoreSysRoot = false);
  53. /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
  54. /// libstdc++.
  55. void AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
  56. llvm::StringRef ArchDir,
  57. llvm::StringRef Dir32,
  58. llvm::StringRef Dir64,
  59. const llvm::Triple &triple);
  60. /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to suport a MinGW
  61. /// libstdc++.
  62. void AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
  63. llvm::StringRef Arch,
  64. llvm::StringRef Version);
  65. /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
  66. /// separator. The processing follows that of the CPATH variable for gcc.
  67. void AddDelimitedPaths(llvm::StringRef String);
  68. // AddDefaultCIncludePaths - Add paths that should always be searched.
  69. void AddDefaultCIncludePaths(const llvm::Triple &triple,
  70. const HeaderSearchOptions &HSOpts);
  71. // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
  72. // compiling c++.
  73. void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple);
  74. /// AddDefaultSystemIncludePaths - Adds the default system include paths so
  75. /// that e.g. stdio.h is found.
  76. void AddDefaultSystemIncludePaths(const LangOptions &Lang,
  77. const llvm::Triple &triple,
  78. const HeaderSearchOptions &HSOpts);
  79. /// Realize - Merges all search path lists into one list and send it to
  80. /// HeaderSearch.
  81. void Realize();
  82. };
  83. }
  84. void InitHeaderSearch::AddPath(const llvm::Twine &Path,
  85. IncludeDirGroup Group, bool isCXXAware,
  86. bool isUserSupplied, bool isFramework,
  87. bool IgnoreSysRoot) {
  88. assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
  89. FileManager &FM = Headers.getFileMgr();
  90. // Compute the actual path, taking into consideration -isysroot.
  91. llvm::SmallString<256> MappedPathStorage;
  92. llvm::StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
  93. llvm::sys::Path MappedPath(MappedPathStr);
  94. // Handle isysroot.
  95. if (Group == System && !IgnoreSysRoot && MappedPath.isAbsolute() &&
  96. IncludeSysroot.isValid() &&
  97. IncludeSysroot != llvm::sys::Path::GetRootDirectory()) {
  98. MappedPathStorage.clear();
  99. MappedPathStr =
  100. (IncludeSysroot.str() + Path).toStringRef(MappedPathStorage);
  101. }
  102. // Compute the DirectoryLookup type.
  103. SrcMgr::CharacteristicKind Type;
  104. if (Group == Quoted || Group == Angled)
  105. Type = SrcMgr::C_User;
  106. else if (isCXXAware)
  107. Type = SrcMgr::C_System;
  108. else
  109. Type = SrcMgr::C_ExternCSystem;
  110. // If the directory exists, add it.
  111. if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
  112. IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
  113. isFramework));
  114. return;
  115. }
  116. // Check to see if this is an apple-style headermap (which are not allowed to
  117. // be frameworks).
  118. if (!isFramework) {
  119. if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
  120. if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
  121. // It is a headermap, add it to the search path.
  122. IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
  123. return;
  124. }
  125. }
  126. }
  127. if (Verbose)
  128. llvm::errs() << "ignoring nonexistent directory \""
  129. << MappedPathStr << "\"\n";
  130. }
  131. void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) {
  132. if (at.empty()) // Empty string should not add '.' path.
  133. return;
  134. llvm::StringRef::size_type delim;
  135. while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) {
  136. if (delim == 0)
  137. AddPath(".", Angled, false, true, false);
  138. else
  139. AddPath(at.substr(0, delim), Angled, false, true, false);
  140. at = at.substr(delim + 1);
  141. }
  142. if (at.empty())
  143. AddPath(".", Angled, false, true, false);
  144. else
  145. AddPath(at, Angled, false, true, false);
  146. }
  147. void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
  148. llvm::StringRef ArchDir,
  149. llvm::StringRef Dir32,
  150. llvm::StringRef Dir64,
  151. const llvm::Triple &triple) {
  152. // Add the base dir
  153. AddPath(Base, System, true, false, false);
  154. // Add the multilib dirs
  155. llvm::Triple::ArchType arch = triple.getArch();
  156. bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
  157. if (is64bit)
  158. AddPath(Base + "/" + ArchDir + "/" + Dir64, System, true, false, false);
  159. else
  160. AddPath(Base + "/" + ArchDir + "/" + Dir32, System, true, false, false);
  161. // Add the backward dir
  162. AddPath(Base + "/backward", System, true, false, false);
  163. }
  164. void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
  165. llvm::StringRef Arch,
  166. llvm::StringRef Version) {
  167. AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
  168. System, true, false, false);
  169. AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
  170. System, true, false, false);
  171. AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
  172. System, true, false, false);
  173. }
  174. // FIXME: This probably should goto to some platform utils place.
  175. #ifdef _MSC_VER
  176. // Read registry string.
  177. // This also supports a means to look for high-versioned keys by use
  178. // of a $VERSION placeholder in the key path.
  179. // $VERSION in the key path is a placeholder for the version number,
  180. // causing the highest value path to be searched for and used.
  181. // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
  182. // There can be additional characters in the component. Only the numberic
  183. // characters are compared.
  184. static bool getSystemRegistryString(const char *keyPath, const char *valueName,
  185. char *value, size_t maxLength) {
  186. HKEY hRootKey = NULL;
  187. HKEY hKey = NULL;
  188. const char* subKey = NULL;
  189. DWORD valueType;
  190. DWORD valueSize = maxLength - 1;
  191. long lResult;
  192. bool returnValue = false;
  193. if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
  194. hRootKey = HKEY_CLASSES_ROOT;
  195. subKey = keyPath + 18;
  196. }
  197. else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
  198. hRootKey = HKEY_USERS;
  199. subKey = keyPath + 11;
  200. }
  201. else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
  202. hRootKey = HKEY_LOCAL_MACHINE;
  203. subKey = keyPath + 19;
  204. }
  205. else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
  206. hRootKey = HKEY_CURRENT_USER;
  207. subKey = keyPath + 18;
  208. }
  209. else
  210. return(false);
  211. const char *placeHolder = strstr(subKey, "$VERSION");
  212. char bestName[256];
  213. bestName[0] = '\0';
  214. // If we have a $VERSION placeholder, do the highest-version search.
  215. if (placeHolder) {
  216. const char *keyEnd = placeHolder - 1;
  217. const char *nextKey = placeHolder;
  218. // Find end of previous key.
  219. while ((keyEnd > subKey) && (*keyEnd != '\\'))
  220. keyEnd--;
  221. // Find end of key containing $VERSION.
  222. while (*nextKey && (*nextKey != '\\'))
  223. nextKey++;
  224. size_t partialKeyLength = keyEnd - subKey;
  225. char partialKey[256];
  226. if (partialKeyLength > sizeof(partialKey))
  227. partialKeyLength = sizeof(partialKey);
  228. strncpy(partialKey, subKey, partialKeyLength);
  229. partialKey[partialKeyLength] = '\0';
  230. HKEY hTopKey = NULL;
  231. lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
  232. if (lResult == ERROR_SUCCESS) {
  233. char keyName[256];
  234. int bestIndex = -1;
  235. double bestValue = 0.0;
  236. DWORD index, size = sizeof(keyName) - 1;
  237. for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
  238. NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
  239. const char *sp = keyName;
  240. while (*sp && !isdigit(*sp))
  241. sp++;
  242. if (!*sp)
  243. continue;
  244. const char *ep = sp + 1;
  245. while (*ep && (isdigit(*ep) || (*ep == '.')))
  246. ep++;
  247. char numBuf[32];
  248. strncpy(numBuf, sp, sizeof(numBuf) - 1);
  249. numBuf[sizeof(numBuf) - 1] = '\0';
  250. double value = strtod(numBuf, NULL);
  251. if (value > bestValue) {
  252. bestIndex = (int)index;
  253. bestValue = value;
  254. strcpy(bestName, keyName);
  255. }
  256. size = sizeof(keyName) - 1;
  257. }
  258. // If we found the highest versioned key, open the key and get the value.
  259. if (bestIndex != -1) {
  260. // Append rest of key.
  261. strncat(bestName, nextKey, sizeof(bestName) - 1);
  262. bestName[sizeof(bestName) - 1] = '\0';
  263. // Open the chosen key path remainder.
  264. lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
  265. if (lResult == ERROR_SUCCESS) {
  266. lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
  267. (LPBYTE)value, &valueSize);
  268. if (lResult == ERROR_SUCCESS)
  269. returnValue = true;
  270. RegCloseKey(hKey);
  271. }
  272. }
  273. RegCloseKey(hTopKey);
  274. }
  275. }
  276. else {
  277. lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
  278. if (lResult == ERROR_SUCCESS) {
  279. lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
  280. (LPBYTE)value, &valueSize);
  281. if (lResult == ERROR_SUCCESS)
  282. returnValue = true;
  283. RegCloseKey(hKey);
  284. }
  285. }
  286. return(returnValue);
  287. }
  288. #else // _MSC_VER
  289. // Read registry string.
  290. static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
  291. return(false);
  292. }
  293. #endif // _MSC_VER
  294. // Get Visual Studio installation directory.
  295. static bool getVisualStudioDir(std::string &path) {
  296. // First check the environment variables that vsvars32.bat sets.
  297. const char* vcinstalldir = getenv("VCINSTALLDIR");
  298. if(vcinstalldir) {
  299. char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
  300. if (p)
  301. *p = '\0';
  302. path = vcinstalldir;
  303. return(true);
  304. }
  305. char vsIDEInstallDir[256];
  306. char vsExpressIDEInstallDir[256];
  307. // Then try the windows registry.
  308. bool hasVCDir = getSystemRegistryString(
  309. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
  310. "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
  311. bool hasVCExpressDir = getSystemRegistryString(
  312. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
  313. "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
  314. // If we have both vc80 and vc90, pick version we were compiled with.
  315. if (hasVCDir && vsIDEInstallDir[0]) {
  316. char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
  317. if (p)
  318. *p = '\0';
  319. path = vsIDEInstallDir;
  320. return(true);
  321. }
  322. else if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
  323. char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
  324. if (p)
  325. *p = '\0';
  326. path = vsExpressIDEInstallDir;
  327. return(true);
  328. }
  329. else {
  330. // Try the environment.
  331. const char* vs100comntools = getenv("VS100COMNTOOLS");
  332. const char* vs90comntools = getenv("VS90COMNTOOLS");
  333. const char* vs80comntools = getenv("VS80COMNTOOLS");
  334. const char* vscomntools = NULL;
  335. // Try to find the version that we were compiled with
  336. if(false) {}
  337. #if (_MSC_VER >= 1600) // VC100
  338. else if(vs100comntools) {
  339. vscomntools = vs100comntools;
  340. }
  341. #elif (_MSC_VER == 1500) // VC80
  342. else if(vs90comntools) {
  343. vscomntools = vs90comntools;
  344. }
  345. #elif (_MSC_VER == 1400) // VC80
  346. else if(vs80comntools) {
  347. vscomntools = vs80comntools;
  348. }
  349. #endif
  350. // Otherwise find any version we can
  351. else if (vs100comntools)
  352. vscomntools = vs100comntools;
  353. else if (vs90comntools)
  354. vscomntools = vs90comntools;
  355. else if (vs80comntools)
  356. vscomntools = vs80comntools;
  357. if (vscomntools && *vscomntools) {
  358. char *p = const_cast<char *>(strstr(vscomntools, "\\Common7\\Tools"));
  359. if (p)
  360. *p = '\0';
  361. path = vscomntools;
  362. return(true);
  363. }
  364. else
  365. return(false);
  366. }
  367. return(false);
  368. }
  369. // Get Windows SDK installation directory.
  370. static bool getWindowsSDKDir(std::string &path) {
  371. char windowsSDKInstallDir[256];
  372. // Try the Windows registry.
  373. bool hasSDKDir = getSystemRegistryString(
  374. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
  375. "InstallationFolder", windowsSDKInstallDir, sizeof(windowsSDKInstallDir) - 1);
  376. // If we have both vc80 and vc90, pick version we were compiled with.
  377. if (hasSDKDir && windowsSDKInstallDir[0]) {
  378. path = windowsSDKInstallDir;
  379. return(true);
  380. }
  381. return(false);
  382. }
  383. void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
  384. const HeaderSearchOptions &HSOpts) {
  385. // FIXME: temporary hack: hard-coded paths.
  386. AddPath("/usr/local/include", System, true, false, false);
  387. // Builtin includes use #include_next directives and should be positioned
  388. // just prior C include dirs.
  389. if (HSOpts.UseBuiltinIncludes) {
  390. // Ignore the sys root, we *always* look for clang headers relative to
  391. // supplied path.
  392. llvm::sys::Path P(HSOpts.ResourceDir);
  393. P.appendComponent("include");
  394. AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
  395. }
  396. // Add dirs specified via 'configure --with-c-include-dirs'.
  397. llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
  398. if (CIncludeDirs != "") {
  399. llvm::SmallVector<llvm::StringRef, 5> dirs;
  400. CIncludeDirs.split(dirs, ":");
  401. for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
  402. i != dirs.end();
  403. ++i)
  404. AddPath(*i, System, false, false, false);
  405. return;
  406. }
  407. llvm::Triple::OSType os = triple.getOS();
  408. switch (os) {
  409. case llvm::Triple::Win32: {
  410. std::string VSDir;
  411. std::string WindowsSDKDir;
  412. if (getVisualStudioDir(VSDir)) {
  413. AddPath(VSDir + "\\VC\\include", System, false, false, false);
  414. if (getWindowsSDKDir(WindowsSDKDir))
  415. AddPath(WindowsSDKDir + "\\include", System, false, false, false);
  416. else
  417. AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
  418. System, false, false, false);
  419. } else {
  420. // Default install paths.
  421. AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
  422. System, false, false, false);
  423. AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
  424. System, false, false, false);
  425. AddPath(
  426. "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
  427. System, false, false, false);
  428. AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
  429. System, false, false, false);
  430. AddPath(
  431. "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
  432. System, false, false, false);
  433. // For some clang developers.
  434. AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
  435. System, false, false, false);
  436. AddPath(
  437. "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
  438. System, false, false, false);
  439. }
  440. break;
  441. }
  442. case llvm::Triple::Haiku:
  443. AddPath("/boot/common/include", System, true, false, false);
  444. AddPath("/boot/develop/headers/os", System, true, false, false);
  445. AddPath("/boot/develop/headers/os/app", System, true, false, false);
  446. AddPath("/boot/develop/headers/os/arch", System, true, false, false);
  447. AddPath("/boot/develop/headers/os/device", System, true, false, false);
  448. AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
  449. AddPath("/boot/develop/headers/os/game", System, true, false, false);
  450. AddPath("/boot/develop/headers/os/interface", System, true, false, false);
  451. AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
  452. AddPath("/boot/develop/headers/os/locale", System, true, false, false);
  453. AddPath("/boot/develop/headers/os/mail", System, true, false, false);
  454. AddPath("/boot/develop/headers/os/media", System, true, false, false);
  455. AddPath("/boot/develop/headers/os/midi", System, true, false, false);
  456. AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
  457. AddPath("/boot/develop/headers/os/net", System, true, false, false);
  458. AddPath("/boot/develop/headers/os/storage", System, true, false, false);
  459. AddPath("/boot/develop/headers/os/support", System, true, false, false);
  460. AddPath("/boot/develop/headers/os/translation",
  461. System, true, false, false);
  462. AddPath("/boot/develop/headers/os/add-ons/graphics",
  463. System, true, false, false);
  464. AddPath("/boot/develop/headers/os/add-ons/input_server",
  465. System, true, false, false);
  466. AddPath("/boot/develop/headers/os/add-ons/screen_saver",
  467. System, true, false, false);
  468. AddPath("/boot/develop/headers/os/add-ons/tracker",
  469. System, true, false, false);
  470. AddPath("/boot/develop/headers/os/be_apps/Deskbar",
  471. System, true, false, false);
  472. AddPath("/boot/develop/headers/os/be_apps/NetPositive",
  473. System, true, false, false);
  474. AddPath("/boot/develop/headers/os/be_apps/Tracker",
  475. System, true, false, false);
  476. AddPath("/boot/develop/headers/cpp", System, true, false, false);
  477. AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
  478. System, true, false, false);
  479. AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
  480. AddPath("/boot/develop/headers/bsd", System, true, false, false);
  481. AddPath("/boot/develop/headers/glibc", System, true, false, false);
  482. AddPath("/boot/develop/headers/posix", System, true, false, false);
  483. AddPath("/boot/develop/headers", System, true, false, false);
  484. break;
  485. case llvm::Triple::Cygwin:
  486. AddPath("/usr/include/w32api", System, true, false, false);
  487. break;
  488. case llvm::Triple::MinGW64:
  489. case llvm::Triple::MinGW32:
  490. AddPath("c:/mingw/include", System, true, false, false);
  491. break;
  492. default:
  493. break;
  494. }
  495. AddPath("/usr/include", System, false, false, false);
  496. }
  497. void InitHeaderSearch::
  498. AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) {
  499. llvm::Triple::OSType os = triple.getOS();
  500. llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
  501. if (CxxIncludeRoot != "") {
  502. llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
  503. if (CxxIncludeArch == "")
  504. AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
  505. CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
  506. triple);
  507. else
  508. AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
  509. CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
  510. triple);
  511. return;
  512. }
  513. // FIXME: temporary hack: hard-coded paths.
  514. switch (os) {
  515. case llvm::Triple::Cygwin:
  516. // Cygwin-1.7
  517. AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
  518. // g++-4 / Cygwin-1.5
  519. AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
  520. // FIXME: Do we support g++-3.4.4?
  521. AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
  522. break;
  523. case llvm::Triple::MinGW64:
  524. // Try gcc 4.5.0
  525. AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.5.0");
  526. // Try gcc 4.4.0
  527. AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
  528. // Try gcc 4.3.0
  529. AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
  530. // Fall through.
  531. case llvm::Triple::MinGW32:
  532. // Try gcc 4.5.0
  533. AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
  534. // Try gcc 4.4.0
  535. AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
  536. // Try gcc 4.3.0
  537. AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
  538. break;
  539. case llvm::Triple::Darwin:
  540. switch (triple.getArch()) {
  541. default: break;
  542. case llvm::Triple::ppc:
  543. case llvm::Triple::ppc64:
  544. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
  545. "powerpc-apple-darwin10", "", "ppc64",
  546. triple);
  547. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
  548. "powerpc-apple-darwin10", "", "ppc64",
  549. triple);
  550. break;
  551. case llvm::Triple::x86:
  552. case llvm::Triple::x86_64:
  553. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
  554. "i686-apple-darwin10", "", "x86_64", triple);
  555. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
  556. "i686-apple-darwin8", "", "", triple);
  557. break;
  558. case llvm::Triple::arm:
  559. case llvm::Triple::thumb:
  560. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
  561. "arm-apple-darwin10", "v7", "", triple);
  562. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
  563. "arm-apple-darwin10", "v6", "", triple);
  564. break;
  565. }
  566. break;
  567. case llvm::Triple::DragonFly:
  568. AddPath("/usr/include/c++/4.1", System, true, false, false);
  569. break;
  570. case llvm::Triple::Linux:
  571. //===------------------------------------------------------------------===//
  572. // Debian based distros.
  573. // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
  574. //===------------------------------------------------------------------===//
  575. // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
  576. // Ubuntu 9.10 "Karmic Koala" -- gcc-4.4.1
  577. // Debian 6.0 "squeeze" -- gcc-4.4.2
  578. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
  579. "x86_64-linux-gnu", "32", "", triple);
  580. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
  581. "i486-linux-gnu", "", "64", triple);
  582. // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
  583. // Ubuntu 8.10 "Intrepid Ibex" -- gcc-4.3.2
  584. // Debian 5.0 "lenny" -- gcc-4.3.2
  585. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
  586. "x86_64-linux-gnu", "32", "", triple);
  587. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
  588. "i486-linux-gnu", "", "64", triple);
  589. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
  590. "arm-linux-gnueabi", "", "", triple);
  591. // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4
  592. // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
  593. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
  594. "x86_64-linux-gnu", "32", "", triple);
  595. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
  596. "i486-linux-gnu", "", "64", triple);
  597. // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
  598. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
  599. "x86_64-linux-gnu", "32", "", triple);
  600. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
  601. "i486-linux-gnu", "", "64", triple);
  602. //===------------------------------------------------------------------===//
  603. // Redhat based distros.
  604. //===------------------------------------------------------------------===//
  605. // Fedora 14
  606. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
  607. "x86_64-redhat-linux", "32", "", triple);
  608. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
  609. "i686-redhat-linux", "", "", triple);
  610. // Fedora 13
  611. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
  612. "x86_64-redhat-linux", "32", "", triple);
  613. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
  614. "i686-redhat-linux","", "", triple);
  615. // Fedora 12
  616. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
  617. "x86_64-redhat-linux", "32", "", triple);
  618. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
  619. "i686-redhat-linux","", "", triple);
  620. // Fedora 12 (pre-FEB-2010)
  621. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
  622. "x86_64-redhat-linux", "32", "", triple);
  623. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
  624. "i686-redhat-linux","", "", triple);
  625. // Fedora 11
  626. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
  627. "x86_64-redhat-linux", "32", "", triple);
  628. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
  629. "i586-redhat-linux","", "", triple);
  630. // Fedora 10
  631. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
  632. "x86_64-redhat-linux", "32", "", triple);
  633. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
  634. "i386-redhat-linux","", "", triple);
  635. // Fedora 9
  636. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
  637. "x86_64-redhat-linux", "32", "", triple);
  638. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
  639. "i386-redhat-linux", "", "", triple);
  640. // Fedora 8
  641. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
  642. "x86_64-redhat-linux", "", "", triple);
  643. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
  644. "i386-redhat-linux", "", "", triple);
  645. //===------------------------------------------------------------------===//
  646. // Exherbo (2010-01-25)
  647. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
  648. "x86_64-pc-linux-gnu", "32", "", triple);
  649. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
  650. "i686-pc-linux-gnu", "", "", triple);
  651. // openSUSE 11.1 32 bit
  652. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
  653. "i586-suse-linux", "", "", triple);
  654. // openSUSE 11.1 64 bit
  655. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
  656. "x86_64-suse-linux", "32", "", triple);
  657. // openSUSE 11.2
  658. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
  659. "i586-suse-linux", "", "", triple);
  660. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
  661. "x86_64-suse-linux", "", "", triple);
  662. // Arch Linux 2008-06-24
  663. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
  664. "i686-pc-linux-gnu", "", "", triple);
  665. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
  666. "x86_64-unknown-linux-gnu", "", "", triple);
  667. // Gentoo x86 2010.0 stable
  668. AddGnuCPlusPlusIncludePaths(
  669. "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
  670. "i686-pc-linux-gnu", "", "", triple);
  671. // Gentoo x86 2009.1 stable
  672. AddGnuCPlusPlusIncludePaths(
  673. "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
  674. "i686-pc-linux-gnu", "", "", triple);
  675. // Gentoo x86 2009.0 stable
  676. AddGnuCPlusPlusIncludePaths(
  677. "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
  678. "i686-pc-linux-gnu", "", "", triple);
  679. // Gentoo x86 2008.0 stable
  680. AddGnuCPlusPlusIncludePaths(
  681. "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
  682. "i686-pc-linux-gnu", "", "", triple);
  683. // Gentoo amd64 stable
  684. AddGnuCPlusPlusIncludePaths(
  685. "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
  686. "i686-pc-linux-gnu", "", "", triple);
  687. // Gentoo amd64 gcc 4.3.2
  688. AddGnuCPlusPlusIncludePaths(
  689. "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
  690. "x86_64-pc-linux-gnu", "", "", triple);
  691. // Gentoo amd64 gcc 4.4.3
  692. AddGnuCPlusPlusIncludePaths(
  693. "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
  694. "x86_64-pc-linux-gnu", "32", "", triple);
  695. // Gentoo amd64 gcc 4.4.4
  696. AddGnuCPlusPlusIncludePaths(
  697. "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
  698. "x86_64-pc-linux-gnu", "32", "", triple);
  699. // Gentoo amd64 llvm-gcc trunk
  700. AddGnuCPlusPlusIncludePaths(
  701. "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
  702. "x86_64-pc-linux-gnu", "", "", triple);
  703. break;
  704. case llvm::Triple::FreeBSD:
  705. // FreeBSD 8.0
  706. // FreeBSD 7.3
  707. AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple);
  708. break;
  709. case llvm::Triple::NetBSD:
  710. AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
  711. break;
  712. case llvm::Triple::OpenBSD: {
  713. std::string t = triple.getTriple();
  714. if (t.substr(0, 6) == "x86_64")
  715. t.replace(0, 6, "amd64");
  716. AddGnuCPlusPlusIncludePaths("/usr/include/g++",
  717. t, "", "", triple);
  718. break;
  719. }
  720. case llvm::Triple::Minix:
  721. AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
  722. "", "", "", triple);
  723. break;
  724. case llvm::Triple::Solaris:
  725. // Solaris - Fall though..
  726. case llvm::Triple::AuroraUX:
  727. // AuroraUX
  728. AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
  729. "i386-pc-solaris2.11", "", "", triple);
  730. break;
  731. default:
  732. break;
  733. }
  734. }
  735. void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
  736. const llvm::Triple &triple,
  737. const HeaderSearchOptions &HSOpts) {
  738. if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes) {
  739. if (!HSOpts.CXXSystemIncludes.empty()) {
  740. for (unsigned i = 0, e = HSOpts.CXXSystemIncludes.size(); i != e; ++i)
  741. AddPath(HSOpts.CXXSystemIncludes[i], System, true, false, false);
  742. } else
  743. AddDefaultCPlusPlusIncludePaths(triple);
  744. }
  745. AddDefaultCIncludePaths(triple, HSOpts);
  746. // Add the default framework include paths on Darwin.
  747. if (triple.getOS() == llvm::Triple::Darwin) {
  748. AddPath("/System/Library/Frameworks", System, true, false, true);
  749. AddPath("/Library/Frameworks", System, true, false, true);
  750. }
  751. }
  752. /// RemoveDuplicates - If there are duplicate directory entries in the specified
  753. /// search list, remove the later (dead) ones.
  754. static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
  755. bool Verbose) {
  756. llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
  757. llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
  758. llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
  759. for (unsigned i = 0; i != SearchList.size(); ++i) {
  760. unsigned DirToRemove = i;
  761. const DirectoryLookup &CurEntry = SearchList[i];
  762. if (CurEntry.isNormalDir()) {
  763. // If this isn't the first time we've seen this dir, remove it.
  764. if (SeenDirs.insert(CurEntry.getDir()))
  765. continue;
  766. } else if (CurEntry.isFramework()) {
  767. // If this isn't the first time we've seen this framework dir, remove it.
  768. if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
  769. continue;
  770. } else {
  771. assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
  772. // If this isn't the first time we've seen this headermap, remove it.
  773. if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
  774. continue;
  775. }
  776. // If we have a normal #include dir/framework/headermap that is shadowed
  777. // later in the chain by a system include location, we actually want to
  778. // ignore the user's request and drop the user dir... keeping the system
  779. // dir. This is weird, but required to emulate GCC's search path correctly.
  780. //
  781. // Since dupes of system dirs are rare, just rescan to find the original
  782. // that we're nuking instead of using a DenseMap.
  783. if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
  784. // Find the dir that this is the same of.
  785. unsigned FirstDir;
  786. for (FirstDir = 0; ; ++FirstDir) {
  787. assert(FirstDir != i && "Didn't find dupe?");
  788. const DirectoryLookup &SearchEntry = SearchList[FirstDir];
  789. // If these are different lookup types, then they can't be the dupe.
  790. if (SearchEntry.getLookupType() != CurEntry.getLookupType())
  791. continue;
  792. bool isSame;
  793. if (CurEntry.isNormalDir())
  794. isSame = SearchEntry.getDir() == CurEntry.getDir();
  795. else if (CurEntry.isFramework())
  796. isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
  797. else {
  798. assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
  799. isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
  800. }
  801. if (isSame)
  802. break;
  803. }
  804. // If the first dir in the search path is a non-system dir, zap it
  805. // instead of the system one.
  806. if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
  807. DirToRemove = FirstDir;
  808. }
  809. if (Verbose) {
  810. llvm::errs() << "ignoring duplicate directory \""
  811. << CurEntry.getName() << "\"\n";
  812. if (DirToRemove != i)
  813. llvm::errs() << " as it is a non-system directory that duplicates "
  814. << "a system directory\n";
  815. }
  816. // This is reached if the current entry is a duplicate. Remove the
  817. // DirToRemove (usually the current dir).
  818. SearchList.erase(SearchList.begin()+DirToRemove);
  819. --i;
  820. }
  821. }
  822. void InitHeaderSearch::Realize() {
  823. // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
  824. std::vector<DirectoryLookup> SearchList;
  825. SearchList = IncludeGroup[Angled];
  826. SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
  827. IncludeGroup[System].end());
  828. SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
  829. IncludeGroup[After].end());
  830. RemoveDuplicates(SearchList, Verbose);
  831. RemoveDuplicates(IncludeGroup[Quoted], Verbose);
  832. // Prepend QUOTED list on the search list.
  833. SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
  834. IncludeGroup[Quoted].end());
  835. bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
  836. Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
  837. DontSearchCurDir);
  838. // If verbose, print the list of directories that will be searched.
  839. if (Verbose) {
  840. llvm::errs() << "#include \"...\" search starts here:\n";
  841. unsigned QuotedIdx = IncludeGroup[Quoted].size();
  842. for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
  843. if (i == QuotedIdx)
  844. llvm::errs() << "#include <...> search starts here:\n";
  845. const char *Name = SearchList[i].getName();
  846. const char *Suffix;
  847. if (SearchList[i].isNormalDir())
  848. Suffix = "";
  849. else if (SearchList[i].isFramework())
  850. Suffix = " (framework directory)";
  851. else {
  852. assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
  853. Suffix = " (headermap)";
  854. }
  855. llvm::errs() << " " << Name << Suffix << "\n";
  856. }
  857. llvm::errs() << "End of search list.\n";
  858. }
  859. }
  860. void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
  861. const HeaderSearchOptions &HSOpts,
  862. const LangOptions &Lang,
  863. const llvm::Triple &Triple) {
  864. InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
  865. // Add the user defined entries.
  866. for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
  867. const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
  868. Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
  869. !E.IsSysRootRelative);
  870. }
  871. // Add entries from CPATH and friends.
  872. Init.AddDelimitedPaths(HSOpts.EnvIncPath);
  873. if (Lang.CPlusPlus && Lang.ObjC1)
  874. Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
  875. else if (Lang.CPlusPlus)
  876. Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
  877. else if (Lang.ObjC1)
  878. Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
  879. else
  880. Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
  881. if (HSOpts.UseStandardIncludes)
  882. Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
  883. Init.Realize();
  884. }