xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/WebAssembly.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "WebAssembly.h"
100b57cec5SDimitry Andric #include "CommonArgs.h"
1181ad6265SDimitry Andric #include "Gnu.h"
12480093f4SDimitry Andric #include "clang/Basic/Version.h"
130b57cec5SDimitry Andric #include "clang/Config/config.h"
140b57cec5SDimitry Andric #include "clang/Driver/Compilation.h"
150b57cec5SDimitry Andric #include "clang/Driver/Driver.h"
160b57cec5SDimitry Andric #include "clang/Driver/DriverDiagnostic.h"
170b57cec5SDimitry Andric #include "clang/Driver/Options.h"
1881ad6265SDimitry Andric #include "llvm/Option/ArgList.h"
190b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
200b57cec5SDimitry Andric #include "llvm/Support/Path.h"
2181ad6265SDimitry Andric #include "llvm/Support/VirtualFileSystem.h"
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric using namespace clang::driver;
240b57cec5SDimitry Andric using namespace clang::driver::tools;
250b57cec5SDimitry Andric using namespace clang::driver::toolchains;
260b57cec5SDimitry Andric using namespace clang;
270b57cec5SDimitry Andric using namespace llvm::opt;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric /// Following the conventions in https://wiki.debian.org/Multiarch/Tuples,
300b57cec5SDimitry Andric /// we remove the vendor field to form the multiarch triple.
31fe6060f1SDimitry Andric std::string WebAssembly::getMultiarchTriple(const Driver &D,
320b57cec5SDimitry Andric                                             const llvm::Triple &TargetTriple,
33fe6060f1SDimitry Andric                                             StringRef SysRoot) const {
340b57cec5SDimitry Andric     return (TargetTriple.getArchName() + "-" +
350b57cec5SDimitry Andric             TargetTriple.getOSAndEnvironmentName()).str();
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
390b57cec5SDimitry Andric   const ToolChain &ToolChain = getToolChain();
400b57cec5SDimitry Andric   if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
410b57cec5SDimitry Andric     StringRef UseLinker = A->getValue();
420b57cec5SDimitry Andric     if (!UseLinker.empty()) {
430b57cec5SDimitry Andric       if (llvm::sys::path::is_absolute(UseLinker) &&
440b57cec5SDimitry Andric           llvm::sys::fs::can_execute(UseLinker))
455ffd83dbSDimitry Andric         return std::string(UseLinker);
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric       // Accept 'lld', and 'ld' as aliases for the default linker
480b57cec5SDimitry Andric       if (UseLinker != "lld" && UseLinker != "ld")
490b57cec5SDimitry Andric         ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
500b57cec5SDimitry Andric             << A->getAsString(Args);
510b57cec5SDimitry Andric     }
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   return ToolChain.GetProgramPath(ToolChain.getDefaultLinker());
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
580b57cec5SDimitry Andric                                 const InputInfo &Output,
590b57cec5SDimitry Andric                                 const InputInfoList &Inputs,
600b57cec5SDimitry Andric                                 const ArgList &Args,
610b57cec5SDimitry Andric                                 const char *LinkingOutput) const {
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   const ToolChain &ToolChain = getToolChain();
640b57cec5SDimitry Andric   const char *Linker = Args.MakeArgString(getLinkerPath(Args));
650b57cec5SDimitry Andric   ArgStringList CmdArgs;
660b57cec5SDimitry Andric 
675ffd83dbSDimitry Andric   CmdArgs.push_back("-m");
68349cc55cSDimitry Andric   if (ToolChain.getTriple().isArch64Bit())
695ffd83dbSDimitry Andric     CmdArgs.push_back("wasm64");
705ffd83dbSDimitry Andric   else
715ffd83dbSDimitry Andric     CmdArgs.push_back("wasm32");
725ffd83dbSDimitry Andric 
730b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_s))
740b57cec5SDimitry Andric     CmdArgs.push_back("--strip-all");
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_L);
770b57cec5SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_u);
780b57cec5SDimitry Andric   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
790b57cec5SDimitry Andric 
805ffd83dbSDimitry Andric   const char *Crt1 = "crt1.o";
8104eeddc0SDimitry Andric   const char *Entry = nullptr;
82fe6060f1SDimitry Andric 
83fe6060f1SDimitry Andric   // If crt1-command.o exists, it supports new-style commands, so use it.
84fe6060f1SDimitry Andric   // Otherwise, use the old crt1.o. This is a temporary transition measure.
85fe6060f1SDimitry Andric   // Once WASI libc no longer needs to support LLVM versions which lack
86fe6060f1SDimitry Andric   // support for new-style command, it can make crt1.o the same as
87fe6060f1SDimitry Andric   // crt1-command.o. And once LLVM no longer needs to support WASI libc
88fe6060f1SDimitry Andric   // versions before that, it can switch to using crt1-command.o.
89fe6060f1SDimitry Andric   if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o")
90fe6060f1SDimitry Andric     Crt1 = "crt1-command.o";
91fe6060f1SDimitry Andric 
925ffd83dbSDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) {
935ffd83dbSDimitry Andric     StringRef CM = A->getValue();
945ffd83dbSDimitry Andric     if (CM == "command") {
955ffd83dbSDimitry Andric       // Use default values.
965ffd83dbSDimitry Andric     } else if (CM == "reactor") {
975ffd83dbSDimitry Andric       Crt1 = "crt1-reactor.o";
985ffd83dbSDimitry Andric       Entry = "_initialize";
995ffd83dbSDimitry Andric     } else {
1005ffd83dbSDimitry Andric       ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option)
1015ffd83dbSDimitry Andric           << CM << A->getOption().getName();
1025ffd83dbSDimitry Andric     }
1035ffd83dbSDimitry Andric   }
104*06c3fb27SDimitry Andric   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, options::OPT_shared))
1055ffd83dbSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
1065ffd83dbSDimitry Andric   if (Entry) {
1075ffd83dbSDimitry Andric     CmdArgs.push_back(Args.MakeArgString("--entry"));
1085ffd83dbSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(Entry));
1095ffd83dbSDimitry Andric   }
1100b57cec5SDimitry Andric 
111*06c3fb27SDimitry Andric   if (Args.hasArg(options::OPT_shared))
112*06c3fb27SDimitry Andric     CmdArgs.push_back(Args.MakeArgString("-shared"));
113*06c3fb27SDimitry Andric 
1140b57cec5SDimitry Andric   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
1170b57cec5SDimitry Andric     if (ToolChain.ShouldLinkCXXStdlib(Args))
1180b57cec5SDimitry Andric       ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric     if (Args.hasArg(options::OPT_pthread)) {
1210b57cec5SDimitry Andric       CmdArgs.push_back("-lpthread");
1220b57cec5SDimitry Andric       CmdArgs.push_back("--shared-memory");
1230b57cec5SDimitry Andric     }
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric     CmdArgs.push_back("-lc");
1260b57cec5SDimitry Andric     AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
1270b57cec5SDimitry Andric   }
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   CmdArgs.push_back("-o");
1300b57cec5SDimitry Andric   CmdArgs.push_back(Output.getFilename());
1310b57cec5SDimitry Andric 
132e8d8bef9SDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this,
133e8d8bef9SDimitry Andric                                          ResponseFileSupport::AtFileCurCP(),
134e8d8bef9SDimitry Andric                                          Linker, CmdArgs, Inputs, Output));
135480093f4SDimitry Andric 
136480093f4SDimitry Andric   // When optimizing, if wasm-opt is available, run it.
137480093f4SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
138349cc55cSDimitry Andric     auto WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
139480093f4SDimitry Andric     if (WasmOptPath != "wasm-opt") {
140480093f4SDimitry Andric       StringRef OOpt = "s";
141480093f4SDimitry Andric       if (A->getOption().matches(options::OPT_O4) ||
142480093f4SDimitry Andric           A->getOption().matches(options::OPT_Ofast))
143480093f4SDimitry Andric         OOpt = "4";
144480093f4SDimitry Andric       else if (A->getOption().matches(options::OPT_O0))
145480093f4SDimitry Andric         OOpt = "0";
146480093f4SDimitry Andric       else if (A->getOption().matches(options::OPT_O))
147480093f4SDimitry Andric         OOpt = A->getValue();
148480093f4SDimitry Andric 
149480093f4SDimitry Andric       if (OOpt != "0") {
150480093f4SDimitry Andric         const char *WasmOpt = Args.MakeArgString(WasmOptPath);
151480093f4SDimitry Andric         ArgStringList CmdArgs;
152480093f4SDimitry Andric         CmdArgs.push_back(Output.getFilename());
153480093f4SDimitry Andric         CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
154480093f4SDimitry Andric         CmdArgs.push_back("-o");
155480093f4SDimitry Andric         CmdArgs.push_back(Output.getFilename());
1565ffd83dbSDimitry Andric         C.addCommand(std::make_unique<Command>(
1575ffd83dbSDimitry Andric             JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, CmdArgs,
158e8d8bef9SDimitry Andric             Inputs, Output));
159480093f4SDimitry Andric       }
160480093f4SDimitry Andric     }
161480093f4SDimitry Andric   }
162480093f4SDimitry Andric }
163480093f4SDimitry Andric 
164480093f4SDimitry Andric /// Given a base library directory, append path components to form the
165480093f4SDimitry Andric /// LTO directory.
166480093f4SDimitry Andric static std::string AppendLTOLibDir(const std::string &Dir) {
167480093f4SDimitry Andric     // The version allows the path to be keyed to the specific version of
168480093f4SDimitry Andric     // LLVM in used, as the bitcode format is not stable.
169480093f4SDimitry Andric     return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
1730b57cec5SDimitry Andric                          const llvm::opt::ArgList &Args)
1740b57cec5SDimitry Andric     : ToolChain(D, Triple, Args) {
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric   assert(Triple.isArch32Bit() != Triple.isArch64Bit());
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric   getProgramPaths().push_back(getDriver().getInstalledDir());
1790b57cec5SDimitry Andric 
180480093f4SDimitry Andric   auto SysRoot = getDriver().SysRoot;
1810b57cec5SDimitry Andric   if (getTriple().getOS() == llvm::Triple::UnknownOS) {
1820b57cec5SDimitry Andric     // Theoretically an "unknown" OS should mean no standard libraries, however
1830b57cec5SDimitry Andric     // it could also mean that a custom set of libraries is in use, so just add
1840b57cec5SDimitry Andric     // /lib to the search path. Disable multiarch in this case, to discourage
1850b57cec5SDimitry Andric     // paths containing "unknown" from acquiring meanings.
186480093f4SDimitry Andric     getFilePaths().push_back(SysRoot + "/lib");
1870b57cec5SDimitry Andric   } else {
1880b57cec5SDimitry Andric     const std::string MultiarchTriple =
189480093f4SDimitry Andric         getMultiarchTriple(getDriver(), Triple, SysRoot);
190480093f4SDimitry Andric     if (D.isUsingLTO()) {
191480093f4SDimitry Andric       // For LTO, enable use of lto-enabled sysroot libraries too, if available.
192480093f4SDimitry Andric       // Note that the directory is keyed to the LLVM revision, as LLVM's
193480093f4SDimitry Andric       // bitcode format is not stable.
194480093f4SDimitry Andric       auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
195480093f4SDimitry Andric       getFilePaths().push_back(Dir);
196480093f4SDimitry Andric     }
197480093f4SDimitry Andric     getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
1980b57cec5SDimitry Andric   }
1990b57cec5SDimitry Andric }
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric bool WebAssembly::IsMathErrnoDefault() const { return false; }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric bool WebAssembly::UseObjCMixedDispatch() const { return true; }
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric bool WebAssembly::isPICDefault() const { return false; }
2080b57cec5SDimitry Andric 
209349cc55cSDimitry Andric bool WebAssembly::isPIEDefault(const llvm::opt::ArgList &Args) const {
210349cc55cSDimitry Andric   return false;
211349cc55cSDimitry Andric }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric bool WebAssembly::isPICDefaultForced() const { return false; }
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric bool WebAssembly::hasBlocksRuntime() const { return false; }
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric // TODO: Support profiling.
2180b57cec5SDimitry Andric bool WebAssembly::SupportsProfiling() const { return false; }
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric bool WebAssembly::HasNativeLLVMSupport() const { return true; }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
2230b57cec5SDimitry Andric                                         ArgStringList &CC1Args,
2240b57cec5SDimitry Andric                                         Action::OffloadKind) const {
225480093f4SDimitry Andric   if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
2260b57cec5SDimitry Andric                           options::OPT_fno_use_init_array, true))
227480093f4SDimitry Andric     CC1Args.push_back("-fno-use-init-array");
2280b57cec5SDimitry Andric 
229a7dea167SDimitry Andric   // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
2300b57cec5SDimitry Andric   if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
2310b57cec5SDimitry Andric                          false)) {
2320b57cec5SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
2330b57cec5SDimitry Andric                            false))
2340b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2350b57cec5SDimitry Andric           << "-pthread"
2360b57cec5SDimitry Andric           << "-mno-atomics";
2370b57cec5SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
2380b57cec5SDimitry Andric                            options::OPT_mbulk_memory, false))
2390b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2400b57cec5SDimitry Andric           << "-pthread"
2410b57cec5SDimitry Andric           << "-mno-bulk-memory";
2420b57cec5SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
2430b57cec5SDimitry Andric                            options::OPT_mmutable_globals, false))
2440b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2450b57cec5SDimitry Andric           << "-pthread"
2460b57cec5SDimitry Andric           << "-mno-mutable-globals";
247a7dea167SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext,
248a7dea167SDimitry Andric                            false))
249a7dea167SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
250a7dea167SDimitry Andric           << "-pthread"
251a7dea167SDimitry Andric           << "-mno-sign-ext";
2520b57cec5SDimitry Andric     CC1Args.push_back("-target-feature");
2530b57cec5SDimitry Andric     CC1Args.push_back("+atomics");
2540b57cec5SDimitry Andric     CC1Args.push_back("-target-feature");
2550b57cec5SDimitry Andric     CC1Args.push_back("+bulk-memory");
2560b57cec5SDimitry Andric     CC1Args.push_back("-target-feature");
2570b57cec5SDimitry Andric     CC1Args.push_back("+mutable-globals");
258a7dea167SDimitry Andric     CC1Args.push_back("-target-feature");
259a7dea167SDimitry Andric     CC1Args.push_back("+sign-ext");
260a7dea167SDimitry Andric   }
261a7dea167SDimitry Andric 
262e8d8bef9SDimitry Andric   if (!DriverArgs.hasFlag(options::OPT_mmutable_globals,
263e8d8bef9SDimitry Andric                           options::OPT_mno_mutable_globals, false)) {
264e8d8bef9SDimitry Andric     // -fPIC implies +mutable-globals because the PIC ABI used by the linker
265e8d8bef9SDimitry Andric     // depends on importing and exporting mutable globals.
266e8d8bef9SDimitry Andric     llvm::Reloc::Model RelocationModel;
267e8d8bef9SDimitry Andric     unsigned PICLevel;
268e8d8bef9SDimitry Andric     bool IsPIE;
269e8d8bef9SDimitry Andric     std::tie(RelocationModel, PICLevel, IsPIE) =
270e8d8bef9SDimitry Andric         ParsePICArgs(*this, DriverArgs);
271e8d8bef9SDimitry Andric     if (RelocationModel == llvm::Reloc::PIC_) {
272e8d8bef9SDimitry Andric       if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
273e8d8bef9SDimitry Andric                              options::OPT_mmutable_globals, false)) {
274e8d8bef9SDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
275e8d8bef9SDimitry Andric             << "-fPIC"
276e8d8bef9SDimitry Andric             << "-mno-mutable-globals";
277e8d8bef9SDimitry Andric       }
278e8d8bef9SDimitry Andric       CC1Args.push_back("-target-feature");
279e8d8bef9SDimitry Andric       CC1Args.push_back("+mutable-globals");
280e8d8bef9SDimitry Andric     }
281e8d8bef9SDimitry Andric   }
282e8d8bef9SDimitry Andric 
283a7dea167SDimitry Andric   if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
284a7dea167SDimitry Andric     // '-fwasm-exceptions' is not compatible with '-mno-exception-handling'
285a7dea167SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
286a7dea167SDimitry Andric                            options::OPT_mexception_handing, false))
287a7dea167SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
288a7dea167SDimitry Andric           << "-fwasm-exceptions"
289a7dea167SDimitry Andric           << "-mno-exception-handling";
290a7dea167SDimitry Andric     // '-fwasm-exceptions' is not compatible with
291a7dea167SDimitry Andric     // '-mllvm -enable-emscripten-cxx-exceptions'
292a7dea167SDimitry Andric     for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
293a7dea167SDimitry Andric       if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
294a7dea167SDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
295a7dea167SDimitry Andric             << "-fwasm-exceptions"
296a7dea167SDimitry Andric             << "-mllvm -enable-emscripten-cxx-exceptions";
297a7dea167SDimitry Andric     }
298fe6060f1SDimitry Andric     // '-fwasm-exceptions' implies exception-handling feature
299a7dea167SDimitry Andric     CC1Args.push_back("-target-feature");
300a7dea167SDimitry Andric     CC1Args.push_back("+exception-handling");
301349cc55cSDimitry Andric     // Backend needs -wasm-enable-eh to enable Wasm EH
302349cc55cSDimitry Andric     CC1Args.push_back("-mllvm");
303349cc55cSDimitry Andric     CC1Args.push_back("-wasm-enable-eh");
304fe6060f1SDimitry Andric   }
305fe6060f1SDimitry Andric 
306fe6060f1SDimitry Andric   for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
307fe6060f1SDimitry Andric     StringRef Opt = A->getValue(0);
308fe6060f1SDimitry Andric     if (Opt.startswith("-emscripten-cxx-exceptions-allowed")) {
309fe6060f1SDimitry Andric       // '-mllvm -emscripten-cxx-exceptions-allowed' should be used with
310fe6060f1SDimitry Andric       // '-mllvm -enable-emscripten-cxx-exceptions'
311349cc55cSDimitry Andric       bool EmEHArgExists = false;
312fe6060f1SDimitry Andric       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
313fe6060f1SDimitry Andric         if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") {
314349cc55cSDimitry Andric           EmEHArgExists = true;
315fe6060f1SDimitry Andric           break;
316fe6060f1SDimitry Andric         }
317fe6060f1SDimitry Andric       }
318349cc55cSDimitry Andric       if (!EmEHArgExists)
319fe6060f1SDimitry Andric         getDriver().Diag(diag::err_drv_argument_only_allowed_with)
320fe6060f1SDimitry Andric             << "-mllvm -emscripten-cxx-exceptions-allowed"
321fe6060f1SDimitry Andric             << "-mllvm -enable-emscripten-cxx-exceptions";
322fe6060f1SDimitry Andric 
323fe6060f1SDimitry Andric       // Prevent functions specified in -emscripten-cxx-exceptions-allowed list
324fe6060f1SDimitry Andric       // from being inlined before reaching the wasm backend.
325fe6060f1SDimitry Andric       StringRef FuncNamesStr = Opt.split('=').second;
326fe6060f1SDimitry Andric       SmallVector<StringRef, 4> FuncNames;
327fe6060f1SDimitry Andric       FuncNamesStr.split(FuncNames, ',');
328fe6060f1SDimitry Andric       for (auto Name : FuncNames) {
329fe6060f1SDimitry Andric         CC1Args.push_back("-mllvm");
330fe6060f1SDimitry Andric         CC1Args.push_back(DriverArgs.MakeArgString("--force-attribute=" + Name +
331fe6060f1SDimitry Andric                                                    ":noinline"));
332fe6060f1SDimitry Andric       }
333fe6060f1SDimitry Andric     }
334349cc55cSDimitry Andric 
335349cc55cSDimitry Andric     if (Opt.startswith("-wasm-enable-sjlj")) {
336349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' is not compatible with
337349cc55cSDimitry Andric       // '-mno-exception-handling'
338349cc55cSDimitry Andric       if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
339349cc55cSDimitry Andric                              options::OPT_mexception_handing, false))
340349cc55cSDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
341349cc55cSDimitry Andric             << "-mllvm -wasm-enable-sjlj"
342349cc55cSDimitry Andric             << "-mno-exception-handling";
343349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' is not compatible with
344349cc55cSDimitry Andric       // '-mllvm -enable-emscripten-cxx-exceptions'
345349cc55cSDimitry Andric       // because we don't allow Emscripten EH + Wasm SjLj
346349cc55cSDimitry Andric       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
347349cc55cSDimitry Andric         if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
348349cc55cSDimitry Andric           getDriver().Diag(diag::err_drv_argument_not_allowed_with)
349349cc55cSDimitry Andric               << "-mllvm -wasm-enable-sjlj"
350349cc55cSDimitry Andric               << "-mllvm -enable-emscripten-cxx-exceptions";
351349cc55cSDimitry Andric       }
352349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' is not compatible with
353349cc55cSDimitry Andric       // '-mllvm -enable-emscripten-sjlj'
354349cc55cSDimitry Andric       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
355349cc55cSDimitry Andric         if (StringRef(A->getValue(0)) == "-enable-emscripten-sjlj")
356349cc55cSDimitry Andric           getDriver().Diag(diag::err_drv_argument_not_allowed_with)
357349cc55cSDimitry Andric               << "-mllvm -wasm-enable-sjlj"
358349cc55cSDimitry Andric               << "-mllvm -enable-emscripten-sjlj";
359349cc55cSDimitry Andric       }
360349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' implies exception-handling feature
361349cc55cSDimitry Andric       CC1Args.push_back("-target-feature");
362349cc55cSDimitry Andric       CC1Args.push_back("+exception-handling");
363349cc55cSDimitry Andric       // Backend needs '-exception-model=wasm' to use Wasm EH instructions
364349cc55cSDimitry Andric       CC1Args.push_back("-exception-model=wasm");
365349cc55cSDimitry Andric     }
3660b57cec5SDimitry Andric   }
3670b57cec5SDimitry Andric }
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
3700b57cec5SDimitry Andric   return ToolChain::RLT_CompilerRT;
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric ToolChain::CXXStdlibType
3740b57cec5SDimitry Andric WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
3750b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
3760b57cec5SDimitry Andric     StringRef Value = A->getValue();
37781ad6265SDimitry Andric     if (Value == "libc++")
37881ad6265SDimitry Andric       return ToolChain::CST_Libcxx;
37981ad6265SDimitry Andric     else if (Value == "libstdc++")
38081ad6265SDimitry Andric       return ToolChain::CST_Libstdcxx;
38181ad6265SDimitry Andric     else
3820b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_invalid_stdlib_name)
3830b57cec5SDimitry Andric           << A->getAsString(Args);
3840b57cec5SDimitry Andric   }
3850b57cec5SDimitry Andric   return ToolChain::CST_Libcxx;
3860b57cec5SDimitry Andric }
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
3890b57cec5SDimitry Andric                                             ArgStringList &CC1Args) const {
3900b57cec5SDimitry Andric   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
3910b57cec5SDimitry Andric     return;
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   const Driver &D = getDriver();
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
3960b57cec5SDimitry Andric     SmallString<128> P(D.ResourceDir);
3970b57cec5SDimitry Andric     llvm::sys::path::append(P, "include");
3980b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, P);
3990b57cec5SDimitry Andric   }
4000b57cec5SDimitry Andric 
4010b57cec5SDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
4020b57cec5SDimitry Andric     return;
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric   // Check for configure-time C include directories.
4050b57cec5SDimitry Andric   StringRef CIncludeDirs(C_INCLUDE_DIRS);
4060b57cec5SDimitry Andric   if (CIncludeDirs != "") {
4070b57cec5SDimitry Andric     SmallVector<StringRef, 5> dirs;
4080b57cec5SDimitry Andric     CIncludeDirs.split(dirs, ":");
4090b57cec5SDimitry Andric     for (StringRef dir : dirs) {
4100b57cec5SDimitry Andric       StringRef Prefix =
4115ffd83dbSDimitry Andric           llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
4120b57cec5SDimitry Andric       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
4130b57cec5SDimitry Andric     }
4140b57cec5SDimitry Andric     return;
4150b57cec5SDimitry Andric   }
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric   if (getTriple().getOS() != llvm::Triple::UnknownOS) {
4180b57cec5SDimitry Andric     const std::string MultiarchTriple =
4190b57cec5SDimitry Andric         getMultiarchTriple(D, getTriple(), D.SysRoot);
4200b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
4210b57cec5SDimitry Andric   }
4220b57cec5SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
4230b57cec5SDimitry Andric }
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
4260b57cec5SDimitry Andric                                                ArgStringList &CC1Args) const {
42781ad6265SDimitry Andric 
428bdd1243dSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc, options::OPT_nostdinc,
429bdd1243dSDimitry Andric                         options::OPT_nostdincxx))
43081ad6265SDimitry Andric     return;
43181ad6265SDimitry Andric 
43281ad6265SDimitry Andric   switch (GetCXXStdlibType(DriverArgs)) {
43381ad6265SDimitry Andric   case ToolChain::CST_Libcxx:
43481ad6265SDimitry Andric     addLibCxxIncludePaths(DriverArgs, CC1Args);
43581ad6265SDimitry Andric     break;
43681ad6265SDimitry Andric   case ToolChain::CST_Libstdcxx:
43781ad6265SDimitry Andric     addLibStdCXXIncludePaths(DriverArgs, CC1Args);
43881ad6265SDimitry Andric     break;
4390b57cec5SDimitry Andric   }
4400b57cec5SDimitry Andric }
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
4430b57cec5SDimitry Andric                                       llvm::opt::ArgStringList &CmdArgs) const {
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric   switch (GetCXXStdlibType(Args)) {
4460b57cec5SDimitry Andric   case ToolChain::CST_Libcxx:
4470b57cec5SDimitry Andric     CmdArgs.push_back("-lc++");
448fcaf7f86SDimitry Andric     if (Args.hasArg(options::OPT_fexperimental_library))
449fcaf7f86SDimitry Andric       CmdArgs.push_back("-lc++experimental");
4500b57cec5SDimitry Andric     CmdArgs.push_back("-lc++abi");
4510b57cec5SDimitry Andric     break;
4520b57cec5SDimitry Andric   case ToolChain::CST_Libstdcxx:
45381ad6265SDimitry Andric     CmdArgs.push_back("-lstdc++");
45481ad6265SDimitry Andric     break;
4550b57cec5SDimitry Andric   }
4560b57cec5SDimitry Andric }
4570b57cec5SDimitry Andric 
4580b57cec5SDimitry Andric SanitizerMask WebAssembly::getSupportedSanitizers() const {
4590b57cec5SDimitry Andric   SanitizerMask Res = ToolChain::getSupportedSanitizers();
4600b57cec5SDimitry Andric   if (getTriple().isOSEmscripten()) {
4610b57cec5SDimitry Andric     Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
4620b57cec5SDimitry Andric   }
463*06c3fb27SDimitry Andric   // -fsanitize=function places two words before the function label, which are
464*06c3fb27SDimitry Andric   // -unsupported.
465*06c3fb27SDimitry Andric   Res &= ~SanitizerKind::Function;
4660b57cec5SDimitry Andric   return Res;
4670b57cec5SDimitry Andric }
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric Tool *WebAssembly::buildLinker() const {
4700b57cec5SDimitry Andric   return new tools::wasm::Linker(*this);
4710b57cec5SDimitry Andric }
47281ad6265SDimitry Andric 
47381ad6265SDimitry Andric void WebAssembly::addLibCxxIncludePaths(
47481ad6265SDimitry Andric     const llvm::opt::ArgList &DriverArgs,
47581ad6265SDimitry Andric     llvm::opt::ArgStringList &CC1Args) const {
47681ad6265SDimitry Andric   const Driver &D = getDriver();
47781ad6265SDimitry Andric   std::string SysRoot = computeSysRoot();
47881ad6265SDimitry Andric   std::string LibPath = SysRoot + "/include";
47981ad6265SDimitry Andric   const std::string MultiarchTriple =
48081ad6265SDimitry Andric       getMultiarchTriple(D, getTriple(), SysRoot);
48181ad6265SDimitry Andric   bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
48281ad6265SDimitry Andric 
48381ad6265SDimitry Andric   std::string Version = detectLibcxxVersion(LibPath);
48481ad6265SDimitry Andric   if (Version.empty())
48581ad6265SDimitry Andric     return;
48681ad6265SDimitry Andric 
48781ad6265SDimitry Andric   // First add the per-target include path if the OS is known.
48881ad6265SDimitry Andric   if (IsKnownOs) {
48981ad6265SDimitry Andric     std::string TargetDir = LibPath + "/" + MultiarchTriple + "/c++/" + Version;
49081ad6265SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, TargetDir);
49181ad6265SDimitry Andric   }
49281ad6265SDimitry Andric 
49381ad6265SDimitry Andric   // Second add the generic one.
49481ad6265SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
49581ad6265SDimitry Andric }
49681ad6265SDimitry Andric 
49781ad6265SDimitry Andric void WebAssembly::addLibStdCXXIncludePaths(
49881ad6265SDimitry Andric     const llvm::opt::ArgList &DriverArgs,
49981ad6265SDimitry Andric     llvm::opt::ArgStringList &CC1Args) const {
50081ad6265SDimitry Andric   // We cannot use GCCInstallationDetector here as the sysroot usually does
50181ad6265SDimitry Andric   // not contain a full GCC installation.
50281ad6265SDimitry Andric   // Instead, we search the given sysroot for /usr/include/xx, similar
50381ad6265SDimitry Andric   // to how we do it for libc++.
50481ad6265SDimitry Andric   const Driver &D = getDriver();
50581ad6265SDimitry Andric   std::string SysRoot = computeSysRoot();
50681ad6265SDimitry Andric   std::string LibPath = SysRoot + "/include";
50781ad6265SDimitry Andric   const std::string MultiarchTriple =
50881ad6265SDimitry Andric       getMultiarchTriple(D, getTriple(), SysRoot);
50981ad6265SDimitry Andric   bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
51081ad6265SDimitry Andric 
51181ad6265SDimitry Andric   // This is similar to detectLibcxxVersion()
51281ad6265SDimitry Andric   std::string Version;
51381ad6265SDimitry Andric   {
51481ad6265SDimitry Andric     std::error_code EC;
51581ad6265SDimitry Andric     Generic_GCC::GCCVersion MaxVersion =
51681ad6265SDimitry Andric         Generic_GCC::GCCVersion::Parse("0.0.0");
51781ad6265SDimitry Andric     SmallString<128> Path(LibPath);
51881ad6265SDimitry Andric     llvm::sys::path::append(Path, "c++");
51981ad6265SDimitry Andric     for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Path, EC), LE;
52081ad6265SDimitry Andric          !EC && LI != LE; LI = LI.increment(EC)) {
52181ad6265SDimitry Andric       StringRef VersionText = llvm::sys::path::filename(LI->path());
52281ad6265SDimitry Andric       if (VersionText[0] != 'v') {
52381ad6265SDimitry Andric         auto Version = Generic_GCC::GCCVersion::Parse(VersionText);
52481ad6265SDimitry Andric         if (Version > MaxVersion)
52581ad6265SDimitry Andric           MaxVersion = Version;
52681ad6265SDimitry Andric       }
52781ad6265SDimitry Andric     }
52881ad6265SDimitry Andric     if (MaxVersion.Major > 0)
52981ad6265SDimitry Andric       Version = MaxVersion.Text;
53081ad6265SDimitry Andric   }
53181ad6265SDimitry Andric 
53281ad6265SDimitry Andric   if (Version.empty())
53381ad6265SDimitry Andric     return;
53481ad6265SDimitry Andric 
53581ad6265SDimitry Andric   // First add the per-target include path if the OS is known.
53681ad6265SDimitry Andric   if (IsKnownOs) {
53781ad6265SDimitry Andric     std::string TargetDir = LibPath + "/c++/" + Version + "/" + MultiarchTriple;
53881ad6265SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, TargetDir);
53981ad6265SDimitry Andric   }
54081ad6265SDimitry Andric 
54181ad6265SDimitry Andric   // Second add the generic one.
54281ad6265SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
54381ad6265SDimitry Andric   // Third the backward one.
54481ad6265SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version + "/backward");
54581ad6265SDimitry Andric }
546