InitHeaderSearch.cpp 37 KB

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