xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Driver/ToolChains/WebAssembly.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg 
97330f729Sjoerg #include "WebAssembly.h"
107330f729Sjoerg #include "CommonArgs.h"
11*e038c9c4Sjoerg #include "clang/Basic/Version.h"
127330f729Sjoerg #include "clang/Config/config.h"
137330f729Sjoerg #include "clang/Driver/Compilation.h"
147330f729Sjoerg #include "clang/Driver/Driver.h"
157330f729Sjoerg #include "clang/Driver/DriverDiagnostic.h"
167330f729Sjoerg #include "clang/Driver/Options.h"
177330f729Sjoerg #include "llvm/Support/FileSystem.h"
187330f729Sjoerg #include "llvm/Support/Path.h"
197330f729Sjoerg #include "llvm/Option/ArgList.h"
207330f729Sjoerg 
217330f729Sjoerg using namespace clang::driver;
227330f729Sjoerg using namespace clang::driver::tools;
237330f729Sjoerg using namespace clang::driver::toolchains;
247330f729Sjoerg using namespace clang;
257330f729Sjoerg using namespace llvm::opt;
267330f729Sjoerg 
277330f729Sjoerg /// Following the conventions in https://wiki.debian.org/Multiarch/Tuples,
287330f729Sjoerg /// we remove the vendor field to form the multiarch triple.
getMultiarchTriple(const Driver & D,const llvm::Triple & TargetTriple,StringRef SysRoot) const29*e038c9c4Sjoerg std::string WebAssembly::getMultiarchTriple(const Driver &D,
307330f729Sjoerg                                             const llvm::Triple &TargetTriple,
31*e038c9c4Sjoerg                                             StringRef SysRoot) const {
327330f729Sjoerg     return (TargetTriple.getArchName() + "-" +
337330f729Sjoerg             TargetTriple.getOSAndEnvironmentName()).str();
347330f729Sjoerg }
357330f729Sjoerg 
getLinkerPath(const ArgList & Args) const367330f729Sjoerg std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
377330f729Sjoerg   const ToolChain &ToolChain = getToolChain();
387330f729Sjoerg   if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
397330f729Sjoerg     StringRef UseLinker = A->getValue();
407330f729Sjoerg     if (!UseLinker.empty()) {
417330f729Sjoerg       if (llvm::sys::path::is_absolute(UseLinker) &&
427330f729Sjoerg           llvm::sys::fs::can_execute(UseLinker))
43*e038c9c4Sjoerg         return std::string(UseLinker);
447330f729Sjoerg 
457330f729Sjoerg       // Accept 'lld', and 'ld' as aliases for the default linker
467330f729Sjoerg       if (UseLinker != "lld" && UseLinker != "ld")
477330f729Sjoerg         ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
487330f729Sjoerg             << A->getAsString(Args);
497330f729Sjoerg     }
507330f729Sjoerg   }
517330f729Sjoerg 
527330f729Sjoerg   return ToolChain.GetProgramPath(ToolChain.getDefaultLinker());
537330f729Sjoerg }
547330f729Sjoerg 
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const557330f729Sjoerg void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
567330f729Sjoerg                                 const InputInfo &Output,
577330f729Sjoerg                                 const InputInfoList &Inputs,
587330f729Sjoerg                                 const ArgList &Args,
597330f729Sjoerg                                 const char *LinkingOutput) const {
607330f729Sjoerg 
617330f729Sjoerg   const ToolChain &ToolChain = getToolChain();
627330f729Sjoerg   const char *Linker = Args.MakeArgString(getLinkerPath(Args));
637330f729Sjoerg   ArgStringList CmdArgs;
647330f729Sjoerg 
65*e038c9c4Sjoerg   CmdArgs.push_back("-m");
66*e038c9c4Sjoerg   if (getToolChain().getTriple().isArch64Bit())
67*e038c9c4Sjoerg     CmdArgs.push_back("wasm64");
68*e038c9c4Sjoerg   else
69*e038c9c4Sjoerg     CmdArgs.push_back("wasm32");
70*e038c9c4Sjoerg 
717330f729Sjoerg   if (Args.hasArg(options::OPT_s))
727330f729Sjoerg     CmdArgs.push_back("--strip-all");
737330f729Sjoerg 
747330f729Sjoerg   Args.AddAllArgs(CmdArgs, options::OPT_L);
757330f729Sjoerg   Args.AddAllArgs(CmdArgs, options::OPT_u);
767330f729Sjoerg   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
777330f729Sjoerg 
78*e038c9c4Sjoerg   const char *Crt1 = "crt1.o";
79*e038c9c4Sjoerg   const char *Entry = NULL;
80*e038c9c4Sjoerg 
81*e038c9c4Sjoerg   // If crt1-command.o exists, it supports new-style commands, so use it.
82*e038c9c4Sjoerg   // Otherwise, use the old crt1.o. This is a temporary transition measure.
83*e038c9c4Sjoerg   // Once WASI libc no longer needs to support LLVM versions which lack
84*e038c9c4Sjoerg   // support for new-style command, it can make crt1.o the same as
85*e038c9c4Sjoerg   // crt1-command.o. And once LLVM no longer needs to support WASI libc
86*e038c9c4Sjoerg   // versions before that, it can switch to using crt1-command.o.
87*e038c9c4Sjoerg   if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o")
88*e038c9c4Sjoerg     Crt1 = "crt1-command.o";
89*e038c9c4Sjoerg 
90*e038c9c4Sjoerg   if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) {
91*e038c9c4Sjoerg     StringRef CM = A->getValue();
92*e038c9c4Sjoerg     if (CM == "command") {
93*e038c9c4Sjoerg       // Use default values.
94*e038c9c4Sjoerg     } else if (CM == "reactor") {
95*e038c9c4Sjoerg       Crt1 = "crt1-reactor.o";
96*e038c9c4Sjoerg       Entry = "_initialize";
97*e038c9c4Sjoerg     } else {
98*e038c9c4Sjoerg       ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option)
99*e038c9c4Sjoerg           << CM << A->getOption().getName();
100*e038c9c4Sjoerg     }
101*e038c9c4Sjoerg   }
1027330f729Sjoerg   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
103*e038c9c4Sjoerg     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
104*e038c9c4Sjoerg   if (Entry) {
105*e038c9c4Sjoerg     CmdArgs.push_back(Args.MakeArgString("--entry"));
106*e038c9c4Sjoerg     CmdArgs.push_back(Args.MakeArgString(Entry));
107*e038c9c4Sjoerg   }
1087330f729Sjoerg 
1097330f729Sjoerg   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
1107330f729Sjoerg 
1117330f729Sjoerg   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
1127330f729Sjoerg     if (ToolChain.ShouldLinkCXXStdlib(Args))
1137330f729Sjoerg       ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
1147330f729Sjoerg 
1157330f729Sjoerg     if (Args.hasArg(options::OPT_pthread)) {
1167330f729Sjoerg       CmdArgs.push_back("-lpthread");
1177330f729Sjoerg       CmdArgs.push_back("--shared-memory");
1187330f729Sjoerg     }
1197330f729Sjoerg 
1207330f729Sjoerg     CmdArgs.push_back("-lc");
1217330f729Sjoerg     AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
1227330f729Sjoerg   }
1237330f729Sjoerg 
1247330f729Sjoerg   CmdArgs.push_back("-o");
1257330f729Sjoerg   CmdArgs.push_back(Output.getFilename());
1267330f729Sjoerg 
127*e038c9c4Sjoerg   C.addCommand(std::make_unique<Command>(JA, *this,
128*e038c9c4Sjoerg                                          ResponseFileSupport::AtFileCurCP(),
129*e038c9c4Sjoerg                                          Linker, CmdArgs, Inputs, Output));
130*e038c9c4Sjoerg 
131*e038c9c4Sjoerg   // When optimizing, if wasm-opt is available, run it.
132*e038c9c4Sjoerg   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
133*e038c9c4Sjoerg     auto WasmOptPath = getToolChain().GetProgramPath("wasm-opt");
134*e038c9c4Sjoerg     if (WasmOptPath != "wasm-opt") {
135*e038c9c4Sjoerg       StringRef OOpt = "s";
136*e038c9c4Sjoerg       if (A->getOption().matches(options::OPT_O4) ||
137*e038c9c4Sjoerg           A->getOption().matches(options::OPT_Ofast))
138*e038c9c4Sjoerg         OOpt = "4";
139*e038c9c4Sjoerg       else if (A->getOption().matches(options::OPT_O0))
140*e038c9c4Sjoerg         OOpt = "0";
141*e038c9c4Sjoerg       else if (A->getOption().matches(options::OPT_O))
142*e038c9c4Sjoerg         OOpt = A->getValue();
143*e038c9c4Sjoerg 
144*e038c9c4Sjoerg       if (OOpt != "0") {
145*e038c9c4Sjoerg         const char *WasmOpt = Args.MakeArgString(WasmOptPath);
146*e038c9c4Sjoerg         ArgStringList CmdArgs;
147*e038c9c4Sjoerg         CmdArgs.push_back(Output.getFilename());
148*e038c9c4Sjoerg         CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
149*e038c9c4Sjoerg         CmdArgs.push_back("-o");
150*e038c9c4Sjoerg         CmdArgs.push_back(Output.getFilename());
151*e038c9c4Sjoerg         C.addCommand(std::make_unique<Command>(
152*e038c9c4Sjoerg             JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, CmdArgs,
153*e038c9c4Sjoerg             Inputs, Output));
154*e038c9c4Sjoerg       }
155*e038c9c4Sjoerg     }
156*e038c9c4Sjoerg   }
157*e038c9c4Sjoerg }
158*e038c9c4Sjoerg 
159*e038c9c4Sjoerg /// Given a base library directory, append path components to form the
160*e038c9c4Sjoerg /// LTO directory.
AppendLTOLibDir(const std::string & Dir)161*e038c9c4Sjoerg static std::string AppendLTOLibDir(const std::string &Dir) {
162*e038c9c4Sjoerg     // The version allows the path to be keyed to the specific version of
163*e038c9c4Sjoerg     // LLVM in used, as the bitcode format is not stable.
164*e038c9c4Sjoerg     return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
1657330f729Sjoerg }
1667330f729Sjoerg 
WebAssembly(const Driver & D,const llvm::Triple & Triple,const llvm::opt::ArgList & Args)1677330f729Sjoerg WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
1687330f729Sjoerg                          const llvm::opt::ArgList &Args)
1697330f729Sjoerg     : ToolChain(D, Triple, Args) {
1707330f729Sjoerg 
1717330f729Sjoerg   assert(Triple.isArch32Bit() != Triple.isArch64Bit());
1727330f729Sjoerg 
1737330f729Sjoerg   getProgramPaths().push_back(getDriver().getInstalledDir());
1747330f729Sjoerg 
175*e038c9c4Sjoerg   auto SysRoot = getDriver().SysRoot;
1767330f729Sjoerg   if (getTriple().getOS() == llvm::Triple::UnknownOS) {
1777330f729Sjoerg     // Theoretically an "unknown" OS should mean no standard libraries, however
1787330f729Sjoerg     // it could also mean that a custom set of libraries is in use, so just add
1797330f729Sjoerg     // /lib to the search path. Disable multiarch in this case, to discourage
1807330f729Sjoerg     // paths containing "unknown" from acquiring meanings.
181*e038c9c4Sjoerg     getFilePaths().push_back(SysRoot + "/lib");
1827330f729Sjoerg   } else {
1837330f729Sjoerg     const std::string MultiarchTriple =
184*e038c9c4Sjoerg         getMultiarchTriple(getDriver(), Triple, SysRoot);
185*e038c9c4Sjoerg     if (D.isUsingLTO()) {
186*e038c9c4Sjoerg       // For LTO, enable use of lto-enabled sysroot libraries too, if available.
187*e038c9c4Sjoerg       // Note that the directory is keyed to the LLVM revision, as LLVM's
188*e038c9c4Sjoerg       // bitcode format is not stable.
189*e038c9c4Sjoerg       auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
190*e038c9c4Sjoerg       getFilePaths().push_back(Dir);
191*e038c9c4Sjoerg     }
192*e038c9c4Sjoerg     getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
1937330f729Sjoerg   }
1947330f729Sjoerg }
1957330f729Sjoerg 
IsMathErrnoDefault() const1967330f729Sjoerg bool WebAssembly::IsMathErrnoDefault() const { return false; }
1977330f729Sjoerg 
IsObjCNonFragileABIDefault() const1987330f729Sjoerg bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
1997330f729Sjoerg 
UseObjCMixedDispatch() const2007330f729Sjoerg bool WebAssembly::UseObjCMixedDispatch() const { return true; }
2017330f729Sjoerg 
isPICDefault() const2027330f729Sjoerg bool WebAssembly::isPICDefault() const { return false; }
2037330f729Sjoerg 
isPIEDefault() const2047330f729Sjoerg bool WebAssembly::isPIEDefault() const { return false; }
2057330f729Sjoerg 
isPICDefaultForced() const2067330f729Sjoerg bool WebAssembly::isPICDefaultForced() const { return false; }
2077330f729Sjoerg 
IsIntegratedAssemblerDefault() const2087330f729Sjoerg bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
2097330f729Sjoerg 
hasBlocksRuntime() const2107330f729Sjoerg bool WebAssembly::hasBlocksRuntime() const { return false; }
2117330f729Sjoerg 
2127330f729Sjoerg // TODO: Support profiling.
SupportsProfiling() const2137330f729Sjoerg bool WebAssembly::SupportsProfiling() const { return false; }
2147330f729Sjoerg 
HasNativeLLVMSupport() const2157330f729Sjoerg bool WebAssembly::HasNativeLLVMSupport() const { return true; }
2167330f729Sjoerg 
addClangTargetOptions(const ArgList & DriverArgs,ArgStringList & CC1Args,Action::OffloadKind) const2177330f729Sjoerg void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
2187330f729Sjoerg                                         ArgStringList &CC1Args,
2197330f729Sjoerg                                         Action::OffloadKind) const {
220*e038c9c4Sjoerg   if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
2217330f729Sjoerg                           options::OPT_fno_use_init_array, true))
222*e038c9c4Sjoerg     CC1Args.push_back("-fno-use-init-array");
2237330f729Sjoerg 
2247330f729Sjoerg   // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
2257330f729Sjoerg   if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
2267330f729Sjoerg                          false)) {
2277330f729Sjoerg     if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
2287330f729Sjoerg                            false))
2297330f729Sjoerg       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2307330f729Sjoerg           << "-pthread"
2317330f729Sjoerg           << "-mno-atomics";
2327330f729Sjoerg     if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
2337330f729Sjoerg                            options::OPT_mbulk_memory, false))
2347330f729Sjoerg       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2357330f729Sjoerg           << "-pthread"
2367330f729Sjoerg           << "-mno-bulk-memory";
2377330f729Sjoerg     if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
2387330f729Sjoerg                            options::OPT_mmutable_globals, false))
2397330f729Sjoerg       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2407330f729Sjoerg           << "-pthread"
2417330f729Sjoerg           << "-mno-mutable-globals";
2427330f729Sjoerg     if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext,
2437330f729Sjoerg                            false))
2447330f729Sjoerg       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2457330f729Sjoerg           << "-pthread"
2467330f729Sjoerg           << "-mno-sign-ext";
2477330f729Sjoerg     CC1Args.push_back("-target-feature");
2487330f729Sjoerg     CC1Args.push_back("+atomics");
2497330f729Sjoerg     CC1Args.push_back("-target-feature");
2507330f729Sjoerg     CC1Args.push_back("+bulk-memory");
2517330f729Sjoerg     CC1Args.push_back("-target-feature");
2527330f729Sjoerg     CC1Args.push_back("+mutable-globals");
2537330f729Sjoerg     CC1Args.push_back("-target-feature");
2547330f729Sjoerg     CC1Args.push_back("+sign-ext");
2557330f729Sjoerg   }
2567330f729Sjoerg 
257*e038c9c4Sjoerg   if (!DriverArgs.hasFlag(options::OPT_mmutable_globals,
258*e038c9c4Sjoerg                           options::OPT_mno_mutable_globals, false)) {
259*e038c9c4Sjoerg     // -fPIC implies +mutable-globals because the PIC ABI used by the linker
260*e038c9c4Sjoerg     // depends on importing and exporting mutable globals.
261*e038c9c4Sjoerg     llvm::Reloc::Model RelocationModel;
262*e038c9c4Sjoerg     unsigned PICLevel;
263*e038c9c4Sjoerg     bool IsPIE;
264*e038c9c4Sjoerg     std::tie(RelocationModel, PICLevel, IsPIE) =
265*e038c9c4Sjoerg         ParsePICArgs(*this, DriverArgs);
266*e038c9c4Sjoerg     if (RelocationModel == llvm::Reloc::PIC_) {
267*e038c9c4Sjoerg       if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
268*e038c9c4Sjoerg                              options::OPT_mmutable_globals, false)) {
269*e038c9c4Sjoerg         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
270*e038c9c4Sjoerg             << "-fPIC"
271*e038c9c4Sjoerg             << "-mno-mutable-globals";
272*e038c9c4Sjoerg       }
273*e038c9c4Sjoerg       CC1Args.push_back("-target-feature");
274*e038c9c4Sjoerg       CC1Args.push_back("+mutable-globals");
275*e038c9c4Sjoerg     }
276*e038c9c4Sjoerg   }
277*e038c9c4Sjoerg 
2787330f729Sjoerg   if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
2797330f729Sjoerg     // '-fwasm-exceptions' is not compatible with '-mno-exception-handling'
2807330f729Sjoerg     if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
2817330f729Sjoerg                            options::OPT_mexception_handing, false))
2827330f729Sjoerg       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2837330f729Sjoerg           << "-fwasm-exceptions"
2847330f729Sjoerg           << "-mno-exception-handling";
2857330f729Sjoerg     // '-fwasm-exceptions' is not compatible with
2867330f729Sjoerg     // '-mllvm -enable-emscripten-cxx-exceptions'
2877330f729Sjoerg     for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
2887330f729Sjoerg       if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
2897330f729Sjoerg         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2907330f729Sjoerg             << "-fwasm-exceptions"
2917330f729Sjoerg             << "-mllvm -enable-emscripten-cxx-exceptions";
2927330f729Sjoerg     }
293*e038c9c4Sjoerg     // '-fwasm-exceptions' implies exception-handling feature
2947330f729Sjoerg     CC1Args.push_back("-target-feature");
2957330f729Sjoerg     CC1Args.push_back("+exception-handling");
2967330f729Sjoerg   }
297*e038c9c4Sjoerg 
298*e038c9c4Sjoerg   for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
299*e038c9c4Sjoerg     StringRef Opt = A->getValue(0);
300*e038c9c4Sjoerg     if (Opt.startswith("-emscripten-cxx-exceptions-allowed")) {
301*e038c9c4Sjoerg       // '-mllvm -emscripten-cxx-exceptions-allowed' should be used with
302*e038c9c4Sjoerg       // '-mllvm -enable-emscripten-cxx-exceptions'
303*e038c9c4Sjoerg       bool EmExceptionArgExists = false;
304*e038c9c4Sjoerg       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
305*e038c9c4Sjoerg         if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") {
306*e038c9c4Sjoerg           EmExceptionArgExists = true;
307*e038c9c4Sjoerg           break;
308*e038c9c4Sjoerg         }
309*e038c9c4Sjoerg       }
310*e038c9c4Sjoerg       if (!EmExceptionArgExists)
311*e038c9c4Sjoerg         getDriver().Diag(diag::err_drv_argument_only_allowed_with)
312*e038c9c4Sjoerg             << "-mllvm -emscripten-cxx-exceptions-allowed"
313*e038c9c4Sjoerg             << "-mllvm -enable-emscripten-cxx-exceptions";
314*e038c9c4Sjoerg 
315*e038c9c4Sjoerg       // Prevent functions specified in -emscripten-cxx-exceptions-allowed list
316*e038c9c4Sjoerg       // from being inlined before reaching the wasm backend.
317*e038c9c4Sjoerg       StringRef FuncNamesStr = Opt.split('=').second;
318*e038c9c4Sjoerg       SmallVector<StringRef, 4> FuncNames;
319*e038c9c4Sjoerg       FuncNamesStr.split(FuncNames, ',');
320*e038c9c4Sjoerg       for (auto Name : FuncNames) {
321*e038c9c4Sjoerg         CC1Args.push_back("-mllvm");
322*e038c9c4Sjoerg         CC1Args.push_back(DriverArgs.MakeArgString("--force-attribute=" + Name +
323*e038c9c4Sjoerg                                                    ":noinline"));
324*e038c9c4Sjoerg       }
325*e038c9c4Sjoerg     }
326*e038c9c4Sjoerg   }
3277330f729Sjoerg }
3287330f729Sjoerg 
GetDefaultRuntimeLibType() const3297330f729Sjoerg ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
3307330f729Sjoerg   return ToolChain::RLT_CompilerRT;
3317330f729Sjoerg }
3327330f729Sjoerg 
3337330f729Sjoerg ToolChain::CXXStdlibType
GetCXXStdlibType(const ArgList & Args) const3347330f729Sjoerg WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
3357330f729Sjoerg   if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
3367330f729Sjoerg     StringRef Value = A->getValue();
3377330f729Sjoerg     if (Value != "libc++")
3387330f729Sjoerg       getDriver().Diag(diag::err_drv_invalid_stdlib_name)
3397330f729Sjoerg           << A->getAsString(Args);
3407330f729Sjoerg   }
3417330f729Sjoerg   return ToolChain::CST_Libcxx;
3427330f729Sjoerg }
3437330f729Sjoerg 
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const3447330f729Sjoerg void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
3457330f729Sjoerg                                             ArgStringList &CC1Args) const {
3467330f729Sjoerg   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
3477330f729Sjoerg     return;
3487330f729Sjoerg 
3497330f729Sjoerg   const Driver &D = getDriver();
3507330f729Sjoerg 
3517330f729Sjoerg   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
3527330f729Sjoerg     SmallString<128> P(D.ResourceDir);
3537330f729Sjoerg     llvm::sys::path::append(P, "include");
3547330f729Sjoerg     addSystemInclude(DriverArgs, CC1Args, P);
3557330f729Sjoerg   }
3567330f729Sjoerg 
3577330f729Sjoerg   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
3587330f729Sjoerg     return;
3597330f729Sjoerg 
3607330f729Sjoerg   // Check for configure-time C include directories.
3617330f729Sjoerg   StringRef CIncludeDirs(C_INCLUDE_DIRS);
3627330f729Sjoerg   if (CIncludeDirs != "") {
3637330f729Sjoerg     SmallVector<StringRef, 5> dirs;
3647330f729Sjoerg     CIncludeDirs.split(dirs, ":");
3657330f729Sjoerg     for (StringRef dir : dirs) {
3667330f729Sjoerg       StringRef Prefix =
367*e038c9c4Sjoerg           llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
3687330f729Sjoerg       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
3697330f729Sjoerg     }
3707330f729Sjoerg     return;
3717330f729Sjoerg   }
3727330f729Sjoerg 
3737330f729Sjoerg   if (getTriple().getOS() != llvm::Triple::UnknownOS) {
3747330f729Sjoerg     const std::string MultiarchTriple =
3757330f729Sjoerg         getMultiarchTriple(D, getTriple(), D.SysRoot);
3767330f729Sjoerg     addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
3777330f729Sjoerg   }
3787330f729Sjoerg   addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
3797330f729Sjoerg }
3807330f729Sjoerg 
AddClangCXXStdlibIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const3817330f729Sjoerg void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
3827330f729Sjoerg                                                ArgStringList &CC1Args) const {
3837330f729Sjoerg   if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
3847330f729Sjoerg       !DriverArgs.hasArg(options::OPT_nostdincxx)) {
3857330f729Sjoerg     if (getTriple().getOS() != llvm::Triple::UnknownOS) {
3867330f729Sjoerg       const std::string MultiarchTriple =
3877330f729Sjoerg           getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
3887330f729Sjoerg       addSystemInclude(DriverArgs, CC1Args,
3897330f729Sjoerg                        getDriver().SysRoot + "/include/" + MultiarchTriple +
3907330f729Sjoerg                            "/c++/v1");
3917330f729Sjoerg     }
3927330f729Sjoerg     addSystemInclude(DriverArgs, CC1Args,
3937330f729Sjoerg                      getDriver().SysRoot + "/include/c++/v1");
3947330f729Sjoerg   }
3957330f729Sjoerg }
3967330f729Sjoerg 
AddCXXStdlibLibArgs(const llvm::opt::ArgList & Args,llvm::opt::ArgStringList & CmdArgs) const3977330f729Sjoerg void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
3987330f729Sjoerg                                       llvm::opt::ArgStringList &CmdArgs) const {
3997330f729Sjoerg 
4007330f729Sjoerg   switch (GetCXXStdlibType(Args)) {
4017330f729Sjoerg   case ToolChain::CST_Libcxx:
4027330f729Sjoerg     CmdArgs.push_back("-lc++");
4037330f729Sjoerg     CmdArgs.push_back("-lc++abi");
4047330f729Sjoerg     break;
4057330f729Sjoerg   case ToolChain::CST_Libstdcxx:
4067330f729Sjoerg     llvm_unreachable("invalid stdlib name");
4077330f729Sjoerg   }
4087330f729Sjoerg }
4097330f729Sjoerg 
getSupportedSanitizers() const4107330f729Sjoerg SanitizerMask WebAssembly::getSupportedSanitizers() const {
4117330f729Sjoerg   SanitizerMask Res = ToolChain::getSupportedSanitizers();
4127330f729Sjoerg   if (getTriple().isOSEmscripten()) {
4137330f729Sjoerg     Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
4147330f729Sjoerg   }
4157330f729Sjoerg   return Res;
4167330f729Sjoerg }
4177330f729Sjoerg 
buildLinker() const4187330f729Sjoerg Tool *WebAssembly::buildLinker() const {
4197330f729Sjoerg   return new tools::wasm::Linker(*this);
4207330f729Sjoerg }
421