WebAssembly.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. //===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "WebAssembly.h"
  9. #include "CommonArgs.h"
  10. #include "Gnu.h"
  11. #include "clang/Basic/Version.h"
  12. #include "clang/Config/config.h"
  13. #include "clang/Driver/Compilation.h"
  14. #include "clang/Driver/Driver.h"
  15. #include "clang/Driver/DriverDiagnostic.h"
  16. #include "clang/Driver/Options.h"
  17. #include "llvm/Option/ArgList.h"
  18. #include "llvm/Support/FileSystem.h"
  19. #include "llvm/Support/Path.h"
  20. #include "llvm/Support/VirtualFileSystem.h"
  21. using namespace clang::driver;
  22. using namespace clang::driver::tools;
  23. using namespace clang::driver::toolchains;
  24. using namespace clang;
  25. using namespace llvm::opt;
  26. /// Following the conventions in https://wiki.debian.org/Multiarch/Tuples,
  27. /// we remove the vendor field to form the multiarch triple.
  28. std::string WebAssembly::getMultiarchTriple(const Driver &D,
  29. const llvm::Triple &TargetTriple,
  30. StringRef SysRoot) const {
  31. return (TargetTriple.getArchName() + "-" +
  32. TargetTriple.getOSAndEnvironmentName()).str();
  33. }
  34. std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
  35. const ToolChain &ToolChain = getToolChain();
  36. if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
  37. StringRef UseLinker = A->getValue();
  38. if (!UseLinker.empty()) {
  39. if (llvm::sys::path::is_absolute(UseLinker) &&
  40. llvm::sys::fs::can_execute(UseLinker))
  41. return std::string(UseLinker);
  42. // Accept 'lld', and 'ld' as aliases for the default linker
  43. if (UseLinker != "lld" && UseLinker != "ld")
  44. ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
  45. << A->getAsString(Args);
  46. }
  47. }
  48. return ToolChain.GetProgramPath(ToolChain.getDefaultLinker());
  49. }
  50. void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  51. const InputInfo &Output,
  52. const InputInfoList &Inputs,
  53. const ArgList &Args,
  54. const char *LinkingOutput) const {
  55. const ToolChain &ToolChain = getToolChain();
  56. const char *Linker = Args.MakeArgString(getLinkerPath(Args));
  57. ArgStringList CmdArgs;
  58. CmdArgs.push_back("-m");
  59. if (ToolChain.getTriple().isArch64Bit())
  60. CmdArgs.push_back("wasm64");
  61. else
  62. CmdArgs.push_back("wasm32");
  63. if (Args.hasArg(options::OPT_s))
  64. CmdArgs.push_back("--strip-all");
  65. Args.AddAllArgs(CmdArgs, options::OPT_L);
  66. Args.AddAllArgs(CmdArgs, options::OPT_u);
  67. ToolChain.AddFilePathLibArgs(Args, CmdArgs);
  68. const char *Crt1 = "crt1.o";
  69. const char *Entry = nullptr;
  70. // If crt1-command.o exists, it supports new-style commands, so use it.
  71. // Otherwise, use the old crt1.o. This is a temporary transition measure.
  72. // Once WASI libc no longer needs to support LLVM versions which lack
  73. // support for new-style command, it can make crt1.o the same as
  74. // crt1-command.o. And once LLVM no longer needs to support WASI libc
  75. // versions before that, it can switch to using crt1-command.o.
  76. if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o")
  77. Crt1 = "crt1-command.o";
  78. if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) {
  79. StringRef CM = A->getValue();
  80. if (CM == "command") {
  81. // Use default values.
  82. } else if (CM == "reactor") {
  83. Crt1 = "crt1-reactor.o";
  84. Entry = "_initialize";
  85. } else {
  86. ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option)
  87. << CM << A->getOption().getName();
  88. }
  89. }
  90. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
  91. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
  92. if (Entry) {
  93. CmdArgs.push_back(Args.MakeArgString("--entry"));
  94. CmdArgs.push_back(Args.MakeArgString(Entry));
  95. }
  96. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
  97. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
  98. if (ToolChain.ShouldLinkCXXStdlib(Args))
  99. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  100. if (Args.hasArg(options::OPT_pthread)) {
  101. CmdArgs.push_back("-lpthread");
  102. CmdArgs.push_back("--shared-memory");
  103. }
  104. CmdArgs.push_back("-lc");
  105. AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
  106. }
  107. CmdArgs.push_back("-o");
  108. CmdArgs.push_back(Output.getFilename());
  109. C.addCommand(std::make_unique<Command>(JA, *this,
  110. ResponseFileSupport::AtFileCurCP(),
  111. Linker, CmdArgs, Inputs, Output));
  112. // When optimizing, if wasm-opt is available, run it.
  113. if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
  114. auto WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
  115. if (WasmOptPath != "wasm-opt") {
  116. StringRef OOpt = "s";
  117. if (A->getOption().matches(options::OPT_O4) ||
  118. A->getOption().matches(options::OPT_Ofast))
  119. OOpt = "4";
  120. else if (A->getOption().matches(options::OPT_O0))
  121. OOpt = "0";
  122. else if (A->getOption().matches(options::OPT_O))
  123. OOpt = A->getValue();
  124. if (OOpt != "0") {
  125. const char *WasmOpt = Args.MakeArgString(WasmOptPath);
  126. ArgStringList CmdArgs;
  127. CmdArgs.push_back(Output.getFilename());
  128. CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
  129. CmdArgs.push_back("-o");
  130. CmdArgs.push_back(Output.getFilename());
  131. C.addCommand(std::make_unique<Command>(
  132. JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, CmdArgs,
  133. Inputs, Output));
  134. }
  135. }
  136. }
  137. }
  138. /// Given a base library directory, append path components to form the
  139. /// LTO directory.
  140. static std::string AppendLTOLibDir(const std::string &Dir) {
  141. // The version allows the path to be keyed to the specific version of
  142. // LLVM in used, as the bitcode format is not stable.
  143. return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
  144. }
  145. WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
  146. const llvm::opt::ArgList &Args)
  147. : ToolChain(D, Triple, Args) {
  148. assert(Triple.isArch32Bit() != Triple.isArch64Bit());
  149. getProgramPaths().push_back(getDriver().getInstalledDir());
  150. auto SysRoot = getDriver().SysRoot;
  151. if (getTriple().getOS() == llvm::Triple::UnknownOS) {
  152. // Theoretically an "unknown" OS should mean no standard libraries, however
  153. // it could also mean that a custom set of libraries is in use, so just add
  154. // /lib to the search path. Disable multiarch in this case, to discourage
  155. // paths containing "unknown" from acquiring meanings.
  156. getFilePaths().push_back(SysRoot + "/lib");
  157. } else {
  158. const std::string MultiarchTriple =
  159. getMultiarchTriple(getDriver(), Triple, SysRoot);
  160. if (D.isUsingLTO()) {
  161. // For LTO, enable use of lto-enabled sysroot libraries too, if available.
  162. // Note that the directory is keyed to the LLVM revision, as LLVM's
  163. // bitcode format is not stable.
  164. auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
  165. getFilePaths().push_back(Dir);
  166. }
  167. getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
  168. }
  169. }
  170. bool WebAssembly::IsMathErrnoDefault() const { return false; }
  171. bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
  172. bool WebAssembly::UseObjCMixedDispatch() const { return true; }
  173. bool WebAssembly::isPICDefault() const { return false; }
  174. bool WebAssembly::isPIEDefault(const llvm::opt::ArgList &Args) const {
  175. return false;
  176. }
  177. bool WebAssembly::isPICDefaultForced() const { return false; }
  178. bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
  179. bool WebAssembly::hasBlocksRuntime() const { return false; }
  180. // TODO: Support profiling.
  181. bool WebAssembly::SupportsProfiling() const { return false; }
  182. bool WebAssembly::HasNativeLLVMSupport() const { return true; }
  183. void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
  184. ArgStringList &CC1Args,
  185. Action::OffloadKind) const {
  186. if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
  187. options::OPT_fno_use_init_array, true))
  188. CC1Args.push_back("-fno-use-init-array");
  189. // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
  190. if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
  191. false)) {
  192. if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
  193. false))
  194. getDriver().Diag(diag::err_drv_argument_not_allowed_with)
  195. << "-pthread"
  196. << "-mno-atomics";
  197. if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
  198. options::OPT_mbulk_memory, false))
  199. getDriver().Diag(diag::err_drv_argument_not_allowed_with)
  200. << "-pthread"
  201. << "-mno-bulk-memory";
  202. if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
  203. options::OPT_mmutable_globals, false))
  204. getDriver().Diag(diag::err_drv_argument_not_allowed_with)
  205. << "-pthread"
  206. << "-mno-mutable-globals";
  207. if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext,
  208. false))
  209. getDriver().Diag(diag::err_drv_argument_not_allowed_with)
  210. << "-pthread"
  211. << "-mno-sign-ext";
  212. CC1Args.push_back("-target-feature");
  213. CC1Args.push_back("+atomics");
  214. CC1Args.push_back("-target-feature");
  215. CC1Args.push_back("+bulk-memory");
  216. CC1Args.push_back("-target-feature");
  217. CC1Args.push_back("+mutable-globals");
  218. CC1Args.push_back("-target-feature");
  219. CC1Args.push_back("+sign-ext");
  220. }
  221. if (!DriverArgs.hasFlag(options::OPT_mmutable_globals,
  222. options::OPT_mno_mutable_globals, false)) {
  223. // -fPIC implies +mutable-globals because the PIC ABI used by the linker
  224. // depends on importing and exporting mutable globals.
  225. llvm::Reloc::Model RelocationModel;
  226. unsigned PICLevel;
  227. bool IsPIE;
  228. std::tie(RelocationModel, PICLevel, IsPIE) =
  229. ParsePICArgs(*this, DriverArgs);
  230. if (RelocationModel == llvm::Reloc::PIC_) {
  231. if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
  232. options::OPT_mmutable_globals, false)) {
  233. getDriver().Diag(diag::err_drv_argument_not_allowed_with)
  234. << "-fPIC"
  235. << "-mno-mutable-globals";
  236. }
  237. CC1Args.push_back("-target-feature");
  238. CC1Args.push_back("+mutable-globals");
  239. }
  240. }
  241. if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
  242. // '-fwasm-exceptions' is not compatible with '-mno-exception-handling'
  243. if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
  244. options::OPT_mexception_handing, false))
  245. getDriver().Diag(diag::err_drv_argument_not_allowed_with)
  246. << "-fwasm-exceptions"
  247. << "-mno-exception-handling";
  248. // '-fwasm-exceptions' is not compatible with
  249. // '-mllvm -enable-emscripten-cxx-exceptions'
  250. for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
  251. if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
  252. getDriver().Diag(diag::err_drv_argument_not_allowed_with)
  253. << "-fwasm-exceptions"
  254. << "-mllvm -enable-emscripten-cxx-exceptions";
  255. }
  256. // '-fwasm-exceptions' implies exception-handling feature
  257. CC1Args.push_back("-target-feature");
  258. CC1Args.push_back("+exception-handling");
  259. // Backend needs -wasm-enable-eh to enable Wasm EH
  260. CC1Args.push_back("-mllvm");
  261. CC1Args.push_back("-wasm-enable-eh");
  262. }
  263. for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
  264. StringRef Opt = A->getValue(0);
  265. if (Opt.startswith("-emscripten-cxx-exceptions-allowed")) {
  266. // '-mllvm -emscripten-cxx-exceptions-allowed' should be used with
  267. // '-mllvm -enable-emscripten-cxx-exceptions'
  268. bool EmEHArgExists = false;
  269. for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
  270. if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") {
  271. EmEHArgExists = true;
  272. break;
  273. }
  274. }
  275. if (!EmEHArgExists)
  276. getDriver().Diag(diag::err_drv_argument_only_allowed_with)
  277. << "-mllvm -emscripten-cxx-exceptions-allowed"
  278. << "-mllvm -enable-emscripten-cxx-exceptions";
  279. // Prevent functions specified in -emscripten-cxx-exceptions-allowed list
  280. // from being inlined before reaching the wasm backend.
  281. StringRef FuncNamesStr = Opt.split('=').second;
  282. SmallVector<StringRef, 4> FuncNames;
  283. FuncNamesStr.split(FuncNames, ',');
  284. for (auto Name : FuncNames) {
  285. CC1Args.push_back("-mllvm");
  286. CC1Args.push_back(DriverArgs.MakeArgString("--force-attribute=" + Name +
  287. ":noinline"));
  288. }
  289. }
  290. if (Opt.startswith("-wasm-enable-sjlj")) {
  291. // '-mllvm -wasm-enable-sjlj' is not compatible with
  292. // '-mno-exception-handling'
  293. if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
  294. options::OPT_mexception_handing, false))
  295. getDriver().Diag(diag::err_drv_argument_not_allowed_with)
  296. << "-mllvm -wasm-enable-sjlj"
  297. << "-mno-exception-handling";
  298. // '-mllvm -wasm-enable-sjlj' is not compatible with
  299. // '-mllvm -enable-emscripten-cxx-exceptions'
  300. // because we don't allow Emscripten EH + Wasm SjLj
  301. for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
  302. if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
  303. getDriver().Diag(diag::err_drv_argument_not_allowed_with)
  304. << "-mllvm -wasm-enable-sjlj"
  305. << "-mllvm -enable-emscripten-cxx-exceptions";
  306. }
  307. // '-mllvm -wasm-enable-sjlj' is not compatible with
  308. // '-mllvm -enable-emscripten-sjlj'
  309. for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
  310. if (StringRef(A->getValue(0)) == "-enable-emscripten-sjlj")
  311. getDriver().Diag(diag::err_drv_argument_not_allowed_with)
  312. << "-mllvm -wasm-enable-sjlj"
  313. << "-mllvm -enable-emscripten-sjlj";
  314. }
  315. // '-mllvm -wasm-enable-sjlj' implies exception-handling feature
  316. CC1Args.push_back("-target-feature");
  317. CC1Args.push_back("+exception-handling");
  318. // Backend needs '-exception-model=wasm' to use Wasm EH instructions
  319. CC1Args.push_back("-exception-model=wasm");
  320. }
  321. }
  322. }
  323. ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
  324. return ToolChain::RLT_CompilerRT;
  325. }
  326. ToolChain::CXXStdlibType
  327. WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
  328. if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
  329. StringRef Value = A->getValue();
  330. if (Value == "libc++")
  331. return ToolChain::CST_Libcxx;
  332. else if (Value == "libstdc++")
  333. return ToolChain::CST_Libstdcxx;
  334. else
  335. getDriver().Diag(diag::err_drv_invalid_stdlib_name)
  336. << A->getAsString(Args);
  337. }
  338. return ToolChain::CST_Libcxx;
  339. }
  340. void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
  341. ArgStringList &CC1Args) const {
  342. if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
  343. return;
  344. const Driver &D = getDriver();
  345. if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
  346. SmallString<128> P(D.ResourceDir);
  347. llvm::sys::path::append(P, "include");
  348. addSystemInclude(DriverArgs, CC1Args, P);
  349. }
  350. if (DriverArgs.hasArg(options::OPT_nostdlibinc))
  351. return;
  352. // Check for configure-time C include directories.
  353. StringRef CIncludeDirs(C_INCLUDE_DIRS);
  354. if (CIncludeDirs != "") {
  355. SmallVector<StringRef, 5> dirs;
  356. CIncludeDirs.split(dirs, ":");
  357. for (StringRef dir : dirs) {
  358. StringRef Prefix =
  359. llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
  360. addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
  361. }
  362. return;
  363. }
  364. if (getTriple().getOS() != llvm::Triple::UnknownOS) {
  365. const std::string MultiarchTriple =
  366. getMultiarchTriple(D, getTriple(), D.SysRoot);
  367. addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
  368. }
  369. addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
  370. }
  371. void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
  372. ArgStringList &CC1Args) const {
  373. if (DriverArgs.hasArg(options::OPT_nostdlibinc, options::OPT_nostdinc,
  374. options::OPT_nostdincxx))
  375. return;
  376. switch (GetCXXStdlibType(DriverArgs)) {
  377. case ToolChain::CST_Libcxx:
  378. addLibCxxIncludePaths(DriverArgs, CC1Args);
  379. break;
  380. case ToolChain::CST_Libstdcxx:
  381. addLibStdCXXIncludePaths(DriverArgs, CC1Args);
  382. break;
  383. }
  384. }
  385. void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
  386. llvm::opt::ArgStringList &CmdArgs) const {
  387. switch (GetCXXStdlibType(Args)) {
  388. case ToolChain::CST_Libcxx:
  389. CmdArgs.push_back("-lc++");
  390. if (Args.hasArg(options::OPT_fexperimental_library))
  391. CmdArgs.push_back("-lc++experimental");
  392. CmdArgs.push_back("-lc++abi");
  393. break;
  394. case ToolChain::CST_Libstdcxx:
  395. CmdArgs.push_back("-lstdc++");
  396. break;
  397. }
  398. }
  399. SanitizerMask WebAssembly::getSupportedSanitizers() const {
  400. SanitizerMask Res = ToolChain::getSupportedSanitizers();
  401. if (getTriple().isOSEmscripten()) {
  402. Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
  403. }
  404. return Res;
  405. }
  406. Tool *WebAssembly::buildLinker() const {
  407. return new tools::wasm::Linker(*this);
  408. }
  409. void WebAssembly::addLibCxxIncludePaths(
  410. const llvm::opt::ArgList &DriverArgs,
  411. llvm::opt::ArgStringList &CC1Args) const {
  412. const Driver &D = getDriver();
  413. std::string SysRoot = computeSysRoot();
  414. std::string LibPath = SysRoot + "/include";
  415. const std::string MultiarchTriple =
  416. getMultiarchTriple(D, getTriple(), SysRoot);
  417. bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
  418. std::string Version = detectLibcxxVersion(LibPath);
  419. if (Version.empty())
  420. return;
  421. // First add the per-target include path if the OS is known.
  422. if (IsKnownOs) {
  423. std::string TargetDir = LibPath + "/" + MultiarchTriple + "/c++/" + Version;
  424. addSystemInclude(DriverArgs, CC1Args, TargetDir);
  425. }
  426. // Second add the generic one.
  427. addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
  428. }
  429. void WebAssembly::addLibStdCXXIncludePaths(
  430. const llvm::opt::ArgList &DriverArgs,
  431. llvm::opt::ArgStringList &CC1Args) const {
  432. // We cannot use GCCInstallationDetector here as the sysroot usually does
  433. // not contain a full GCC installation.
  434. // Instead, we search the given sysroot for /usr/include/xx, similar
  435. // to how we do it for libc++.
  436. const Driver &D = getDriver();
  437. std::string SysRoot = computeSysRoot();
  438. std::string LibPath = SysRoot + "/include";
  439. const std::string MultiarchTriple =
  440. getMultiarchTriple(D, getTriple(), SysRoot);
  441. bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
  442. // This is similar to detectLibcxxVersion()
  443. std::string Version;
  444. {
  445. std::error_code EC;
  446. Generic_GCC::GCCVersion MaxVersion =
  447. Generic_GCC::GCCVersion::Parse("0.0.0");
  448. SmallString<128> Path(LibPath);
  449. llvm::sys::path::append(Path, "c++");
  450. for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Path, EC), LE;
  451. !EC && LI != LE; LI = LI.increment(EC)) {
  452. StringRef VersionText = llvm::sys::path::filename(LI->path());
  453. if (VersionText[0] != 'v') {
  454. auto Version = Generic_GCC::GCCVersion::Parse(VersionText);
  455. if (Version > MaxVersion)
  456. MaxVersion = Version;
  457. }
  458. }
  459. if (MaxVersion.Major > 0)
  460. Version = MaxVersion.Text;
  461. }
  462. if (Version.empty())
  463. return;
  464. // First add the per-target include path if the OS is known.
  465. if (IsKnownOs) {
  466. std::string TargetDir = LibPath + "/c++/" + Version + "/" + MultiarchTriple;
  467. addSystemInclude(DriverArgs, CC1Args, TargetDir);
  468. }
  469. // Second add the generic one.
  470. addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
  471. // Third the backward one.
  472. addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version + "/backward");
  473. }