xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/WebAssembly.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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"
11*81ad6265SDimitry 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"
18*81ad6265SDimitry Andric #include "llvm/Option/ArgList.h"
190b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
200b57cec5SDimitry Andric #include "llvm/Support/Path.h"
21*81ad6265SDimitry 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   }
1040b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
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 
1110b57cec5SDimitry Andric   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
1140b57cec5SDimitry Andric     if (ToolChain.ShouldLinkCXXStdlib(Args))
1150b57cec5SDimitry Andric       ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric     if (Args.hasArg(options::OPT_pthread)) {
1180b57cec5SDimitry Andric       CmdArgs.push_back("-lpthread");
1190b57cec5SDimitry Andric       CmdArgs.push_back("--shared-memory");
1200b57cec5SDimitry Andric     }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric     CmdArgs.push_back("-lc");
1230b57cec5SDimitry Andric     AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
1240b57cec5SDimitry Andric   }
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   CmdArgs.push_back("-o");
1270b57cec5SDimitry Andric   CmdArgs.push_back(Output.getFilename());
1280b57cec5SDimitry Andric 
129e8d8bef9SDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this,
130e8d8bef9SDimitry Andric                                          ResponseFileSupport::AtFileCurCP(),
131e8d8bef9SDimitry Andric                                          Linker, CmdArgs, Inputs, Output));
132480093f4SDimitry Andric 
133480093f4SDimitry Andric   // When optimizing, if wasm-opt is available, run it.
134480093f4SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
135349cc55cSDimitry Andric     auto WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
136480093f4SDimitry Andric     if (WasmOptPath != "wasm-opt") {
137480093f4SDimitry Andric       StringRef OOpt = "s";
138480093f4SDimitry Andric       if (A->getOption().matches(options::OPT_O4) ||
139480093f4SDimitry Andric           A->getOption().matches(options::OPT_Ofast))
140480093f4SDimitry Andric         OOpt = "4";
141480093f4SDimitry Andric       else if (A->getOption().matches(options::OPT_O0))
142480093f4SDimitry Andric         OOpt = "0";
143480093f4SDimitry Andric       else if (A->getOption().matches(options::OPT_O))
144480093f4SDimitry Andric         OOpt = A->getValue();
145480093f4SDimitry Andric 
146480093f4SDimitry Andric       if (OOpt != "0") {
147480093f4SDimitry Andric         const char *WasmOpt = Args.MakeArgString(WasmOptPath);
148480093f4SDimitry Andric         ArgStringList CmdArgs;
149480093f4SDimitry Andric         CmdArgs.push_back(Output.getFilename());
150480093f4SDimitry Andric         CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
151480093f4SDimitry Andric         CmdArgs.push_back("-o");
152480093f4SDimitry Andric         CmdArgs.push_back(Output.getFilename());
1535ffd83dbSDimitry Andric         C.addCommand(std::make_unique<Command>(
1545ffd83dbSDimitry Andric             JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, CmdArgs,
155e8d8bef9SDimitry Andric             Inputs, Output));
156480093f4SDimitry Andric       }
157480093f4SDimitry Andric     }
158480093f4SDimitry Andric   }
159480093f4SDimitry Andric }
160480093f4SDimitry Andric 
161480093f4SDimitry Andric /// Given a base library directory, append path components to form the
162480093f4SDimitry Andric /// LTO directory.
163480093f4SDimitry Andric static std::string AppendLTOLibDir(const std::string &Dir) {
164480093f4SDimitry Andric     // The version allows the path to be keyed to the specific version of
165480093f4SDimitry Andric     // LLVM in used, as the bitcode format is not stable.
166480093f4SDimitry Andric     return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
1670b57cec5SDimitry Andric }
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
1700b57cec5SDimitry Andric                          const llvm::opt::ArgList &Args)
1710b57cec5SDimitry Andric     : ToolChain(D, Triple, Args) {
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   assert(Triple.isArch32Bit() != Triple.isArch64Bit());
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   getProgramPaths().push_back(getDriver().getInstalledDir());
1760b57cec5SDimitry Andric 
177480093f4SDimitry Andric   auto SysRoot = getDriver().SysRoot;
1780b57cec5SDimitry Andric   if (getTriple().getOS() == llvm::Triple::UnknownOS) {
1790b57cec5SDimitry Andric     // Theoretically an "unknown" OS should mean no standard libraries, however
1800b57cec5SDimitry Andric     // it could also mean that a custom set of libraries is in use, so just add
1810b57cec5SDimitry Andric     // /lib to the search path. Disable multiarch in this case, to discourage
1820b57cec5SDimitry Andric     // paths containing "unknown" from acquiring meanings.
183480093f4SDimitry Andric     getFilePaths().push_back(SysRoot + "/lib");
1840b57cec5SDimitry Andric   } else {
1850b57cec5SDimitry Andric     const std::string MultiarchTriple =
186480093f4SDimitry Andric         getMultiarchTriple(getDriver(), Triple, SysRoot);
187480093f4SDimitry Andric     if (D.isUsingLTO()) {
188480093f4SDimitry Andric       // For LTO, enable use of lto-enabled sysroot libraries too, if available.
189480093f4SDimitry Andric       // Note that the directory is keyed to the LLVM revision, as LLVM's
190480093f4SDimitry Andric       // bitcode format is not stable.
191480093f4SDimitry Andric       auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
192480093f4SDimitry Andric       getFilePaths().push_back(Dir);
193480093f4SDimitry Andric     }
194480093f4SDimitry Andric     getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
1950b57cec5SDimitry Andric   }
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric bool WebAssembly::IsMathErrnoDefault() const { return false; }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric bool WebAssembly::UseObjCMixedDispatch() const { return true; }
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric bool WebAssembly::isPICDefault() const { return false; }
2050b57cec5SDimitry Andric 
206349cc55cSDimitry Andric bool WebAssembly::isPIEDefault(const llvm::opt::ArgList &Args) const {
207349cc55cSDimitry Andric   return false;
208349cc55cSDimitry Andric }
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric bool WebAssembly::isPICDefaultForced() const { return false; }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric bool WebAssembly::hasBlocksRuntime() const { return false; }
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric // TODO: Support profiling.
2170b57cec5SDimitry Andric bool WebAssembly::SupportsProfiling() const { return false; }
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric bool WebAssembly::HasNativeLLVMSupport() const { return true; }
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
2220b57cec5SDimitry Andric                                         ArgStringList &CC1Args,
2230b57cec5SDimitry Andric                                         Action::OffloadKind) const {
224480093f4SDimitry Andric   if (!DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
2250b57cec5SDimitry Andric                           options::OPT_fno_use_init_array, true))
226480093f4SDimitry Andric     CC1Args.push_back("-fno-use-init-array");
2270b57cec5SDimitry Andric 
228a7dea167SDimitry Andric   // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
2290b57cec5SDimitry Andric   if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
2300b57cec5SDimitry Andric                          false)) {
2310b57cec5SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
2320b57cec5SDimitry Andric                            false))
2330b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2340b57cec5SDimitry Andric           << "-pthread"
2350b57cec5SDimitry Andric           << "-mno-atomics";
2360b57cec5SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
2370b57cec5SDimitry Andric                            options::OPT_mbulk_memory, false))
2380b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2390b57cec5SDimitry Andric           << "-pthread"
2400b57cec5SDimitry Andric           << "-mno-bulk-memory";
2410b57cec5SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
2420b57cec5SDimitry Andric                            options::OPT_mmutable_globals, false))
2430b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
2440b57cec5SDimitry Andric           << "-pthread"
2450b57cec5SDimitry Andric           << "-mno-mutable-globals";
246a7dea167SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext,
247a7dea167SDimitry Andric                            false))
248a7dea167SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
249a7dea167SDimitry Andric           << "-pthread"
250a7dea167SDimitry Andric           << "-mno-sign-ext";
2510b57cec5SDimitry Andric     CC1Args.push_back("-target-feature");
2520b57cec5SDimitry Andric     CC1Args.push_back("+atomics");
2530b57cec5SDimitry Andric     CC1Args.push_back("-target-feature");
2540b57cec5SDimitry Andric     CC1Args.push_back("+bulk-memory");
2550b57cec5SDimitry Andric     CC1Args.push_back("-target-feature");
2560b57cec5SDimitry Andric     CC1Args.push_back("+mutable-globals");
257a7dea167SDimitry Andric     CC1Args.push_back("-target-feature");
258a7dea167SDimitry Andric     CC1Args.push_back("+sign-ext");
259a7dea167SDimitry Andric   }
260a7dea167SDimitry Andric 
261e8d8bef9SDimitry Andric   if (!DriverArgs.hasFlag(options::OPT_mmutable_globals,
262e8d8bef9SDimitry Andric                           options::OPT_mno_mutable_globals, false)) {
263e8d8bef9SDimitry Andric     // -fPIC implies +mutable-globals because the PIC ABI used by the linker
264e8d8bef9SDimitry Andric     // depends on importing and exporting mutable globals.
265e8d8bef9SDimitry Andric     llvm::Reloc::Model RelocationModel;
266e8d8bef9SDimitry Andric     unsigned PICLevel;
267e8d8bef9SDimitry Andric     bool IsPIE;
268e8d8bef9SDimitry Andric     std::tie(RelocationModel, PICLevel, IsPIE) =
269e8d8bef9SDimitry Andric         ParsePICArgs(*this, DriverArgs);
270e8d8bef9SDimitry Andric     if (RelocationModel == llvm::Reloc::PIC_) {
271e8d8bef9SDimitry Andric       if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
272e8d8bef9SDimitry Andric                              options::OPT_mmutable_globals, false)) {
273e8d8bef9SDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
274e8d8bef9SDimitry Andric             << "-fPIC"
275e8d8bef9SDimitry Andric             << "-mno-mutable-globals";
276e8d8bef9SDimitry Andric       }
277e8d8bef9SDimitry Andric       CC1Args.push_back("-target-feature");
278e8d8bef9SDimitry Andric       CC1Args.push_back("+mutable-globals");
279e8d8bef9SDimitry Andric     }
280e8d8bef9SDimitry Andric   }
281e8d8bef9SDimitry Andric 
282a7dea167SDimitry Andric   if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
283a7dea167SDimitry Andric     // '-fwasm-exceptions' is not compatible with '-mno-exception-handling'
284a7dea167SDimitry Andric     if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
285a7dea167SDimitry Andric                            options::OPT_mexception_handing, false))
286a7dea167SDimitry Andric       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
287a7dea167SDimitry Andric           << "-fwasm-exceptions"
288a7dea167SDimitry Andric           << "-mno-exception-handling";
289a7dea167SDimitry Andric     // '-fwasm-exceptions' is not compatible with
290a7dea167SDimitry Andric     // '-mllvm -enable-emscripten-cxx-exceptions'
291a7dea167SDimitry Andric     for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
292a7dea167SDimitry Andric       if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
293a7dea167SDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
294a7dea167SDimitry Andric             << "-fwasm-exceptions"
295a7dea167SDimitry Andric             << "-mllvm -enable-emscripten-cxx-exceptions";
296a7dea167SDimitry Andric     }
297fe6060f1SDimitry Andric     // '-fwasm-exceptions' implies exception-handling feature
298a7dea167SDimitry Andric     CC1Args.push_back("-target-feature");
299a7dea167SDimitry Andric     CC1Args.push_back("+exception-handling");
300349cc55cSDimitry Andric     // Backend needs -wasm-enable-eh to enable Wasm EH
301349cc55cSDimitry Andric     CC1Args.push_back("-mllvm");
302349cc55cSDimitry Andric     CC1Args.push_back("-wasm-enable-eh");
303fe6060f1SDimitry Andric   }
304fe6060f1SDimitry Andric 
305fe6060f1SDimitry Andric   for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
306fe6060f1SDimitry Andric     StringRef Opt = A->getValue(0);
307fe6060f1SDimitry Andric     if (Opt.startswith("-emscripten-cxx-exceptions-allowed")) {
308fe6060f1SDimitry Andric       // '-mllvm -emscripten-cxx-exceptions-allowed' should be used with
309fe6060f1SDimitry Andric       // '-mllvm -enable-emscripten-cxx-exceptions'
310349cc55cSDimitry Andric       bool EmEHArgExists = false;
311fe6060f1SDimitry Andric       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
312fe6060f1SDimitry Andric         if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") {
313349cc55cSDimitry Andric           EmEHArgExists = true;
314fe6060f1SDimitry Andric           break;
315fe6060f1SDimitry Andric         }
316fe6060f1SDimitry Andric       }
317349cc55cSDimitry Andric       if (!EmEHArgExists)
318fe6060f1SDimitry Andric         getDriver().Diag(diag::err_drv_argument_only_allowed_with)
319fe6060f1SDimitry Andric             << "-mllvm -emscripten-cxx-exceptions-allowed"
320fe6060f1SDimitry Andric             << "-mllvm -enable-emscripten-cxx-exceptions";
321fe6060f1SDimitry Andric 
322fe6060f1SDimitry Andric       // Prevent functions specified in -emscripten-cxx-exceptions-allowed list
323fe6060f1SDimitry Andric       // from being inlined before reaching the wasm backend.
324fe6060f1SDimitry Andric       StringRef FuncNamesStr = Opt.split('=').second;
325fe6060f1SDimitry Andric       SmallVector<StringRef, 4> FuncNames;
326fe6060f1SDimitry Andric       FuncNamesStr.split(FuncNames, ',');
327fe6060f1SDimitry Andric       for (auto Name : FuncNames) {
328fe6060f1SDimitry Andric         CC1Args.push_back("-mllvm");
329fe6060f1SDimitry Andric         CC1Args.push_back(DriverArgs.MakeArgString("--force-attribute=" + Name +
330fe6060f1SDimitry Andric                                                    ":noinline"));
331fe6060f1SDimitry Andric       }
332fe6060f1SDimitry Andric     }
333349cc55cSDimitry Andric 
334349cc55cSDimitry Andric     if (Opt.startswith("-wasm-enable-sjlj")) {
335349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' is not compatible with
336349cc55cSDimitry Andric       // '-mno-exception-handling'
337349cc55cSDimitry Andric       if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
338349cc55cSDimitry Andric                              options::OPT_mexception_handing, false))
339349cc55cSDimitry Andric         getDriver().Diag(diag::err_drv_argument_not_allowed_with)
340349cc55cSDimitry Andric             << "-mllvm -wasm-enable-sjlj"
341349cc55cSDimitry Andric             << "-mno-exception-handling";
342349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' is not compatible with
343349cc55cSDimitry Andric       // '-mllvm -enable-emscripten-cxx-exceptions'
344349cc55cSDimitry Andric       // because we don't allow Emscripten EH + Wasm SjLj
345349cc55cSDimitry Andric       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
346349cc55cSDimitry Andric         if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions")
347349cc55cSDimitry Andric           getDriver().Diag(diag::err_drv_argument_not_allowed_with)
348349cc55cSDimitry Andric               << "-mllvm -wasm-enable-sjlj"
349349cc55cSDimitry Andric               << "-mllvm -enable-emscripten-cxx-exceptions";
350349cc55cSDimitry Andric       }
351349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' is not compatible with
352349cc55cSDimitry Andric       // '-mllvm -enable-emscripten-sjlj'
353349cc55cSDimitry Andric       for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
354349cc55cSDimitry Andric         if (StringRef(A->getValue(0)) == "-enable-emscripten-sjlj")
355349cc55cSDimitry Andric           getDriver().Diag(diag::err_drv_argument_not_allowed_with)
356349cc55cSDimitry Andric               << "-mllvm -wasm-enable-sjlj"
357349cc55cSDimitry Andric               << "-mllvm -enable-emscripten-sjlj";
358349cc55cSDimitry Andric       }
359349cc55cSDimitry Andric       // '-mllvm -wasm-enable-sjlj' implies exception-handling feature
360349cc55cSDimitry Andric       CC1Args.push_back("-target-feature");
361349cc55cSDimitry Andric       CC1Args.push_back("+exception-handling");
362349cc55cSDimitry Andric       // Backend needs '-exception-model=wasm' to use Wasm EH instructions
363349cc55cSDimitry Andric       CC1Args.push_back("-exception-model=wasm");
364349cc55cSDimitry Andric     }
3650b57cec5SDimitry Andric   }
3660b57cec5SDimitry Andric }
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
3690b57cec5SDimitry Andric   return ToolChain::RLT_CompilerRT;
3700b57cec5SDimitry Andric }
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric ToolChain::CXXStdlibType
3730b57cec5SDimitry Andric WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
3740b57cec5SDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
3750b57cec5SDimitry Andric     StringRef Value = A->getValue();
376*81ad6265SDimitry Andric     if (Value == "libc++")
377*81ad6265SDimitry Andric       return ToolChain::CST_Libcxx;
378*81ad6265SDimitry Andric     else if (Value == "libstdc++")
379*81ad6265SDimitry Andric       return ToolChain::CST_Libstdcxx;
380*81ad6265SDimitry Andric     else
3810b57cec5SDimitry Andric       getDriver().Diag(diag::err_drv_invalid_stdlib_name)
3820b57cec5SDimitry Andric           << A->getAsString(Args);
3830b57cec5SDimitry Andric   }
3840b57cec5SDimitry Andric   return ToolChain::CST_Libcxx;
3850b57cec5SDimitry Andric }
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
3880b57cec5SDimitry Andric                                             ArgStringList &CC1Args) const {
3890b57cec5SDimitry Andric   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
3900b57cec5SDimitry Andric     return;
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric   const Driver &D = getDriver();
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
3950b57cec5SDimitry Andric     SmallString<128> P(D.ResourceDir);
3960b57cec5SDimitry Andric     llvm::sys::path::append(P, "include");
3970b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, P);
3980b57cec5SDimitry Andric   }
3990b57cec5SDimitry Andric 
4000b57cec5SDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
4010b57cec5SDimitry Andric     return;
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric   // Check for configure-time C include directories.
4040b57cec5SDimitry Andric   StringRef CIncludeDirs(C_INCLUDE_DIRS);
4050b57cec5SDimitry Andric   if (CIncludeDirs != "") {
4060b57cec5SDimitry Andric     SmallVector<StringRef, 5> dirs;
4070b57cec5SDimitry Andric     CIncludeDirs.split(dirs, ":");
4080b57cec5SDimitry Andric     for (StringRef dir : dirs) {
4090b57cec5SDimitry Andric       StringRef Prefix =
4105ffd83dbSDimitry Andric           llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
4110b57cec5SDimitry Andric       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
4120b57cec5SDimitry Andric     }
4130b57cec5SDimitry Andric     return;
4140b57cec5SDimitry Andric   }
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric   if (getTriple().getOS() != llvm::Triple::UnknownOS) {
4170b57cec5SDimitry Andric     const std::string MultiarchTriple =
4180b57cec5SDimitry Andric         getMultiarchTriple(D, getTriple(), D.SysRoot);
4190b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
4200b57cec5SDimitry Andric   }
4210b57cec5SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
4220b57cec5SDimitry Andric }
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
4250b57cec5SDimitry Andric                                                ArgStringList &CC1Args) const {
426*81ad6265SDimitry Andric 
427*81ad6265SDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
428*81ad6265SDimitry Andric       DriverArgs.hasArg(options::OPT_nostdincxx))
429*81ad6265SDimitry Andric     return;
430*81ad6265SDimitry Andric 
431*81ad6265SDimitry Andric   switch (GetCXXStdlibType(DriverArgs)) {
432*81ad6265SDimitry Andric   case ToolChain::CST_Libcxx:
433*81ad6265SDimitry Andric     addLibCxxIncludePaths(DriverArgs, CC1Args);
434*81ad6265SDimitry Andric     break;
435*81ad6265SDimitry Andric   case ToolChain::CST_Libstdcxx:
436*81ad6265SDimitry Andric     addLibStdCXXIncludePaths(DriverArgs, CC1Args);
437*81ad6265SDimitry Andric     break;
4380b57cec5SDimitry Andric   }
4390b57cec5SDimitry Andric }
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
4420b57cec5SDimitry Andric                                       llvm::opt::ArgStringList &CmdArgs) const {
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric   switch (GetCXXStdlibType(Args)) {
4450b57cec5SDimitry Andric   case ToolChain::CST_Libcxx:
4460b57cec5SDimitry Andric     CmdArgs.push_back("-lc++");
4470b57cec5SDimitry Andric     CmdArgs.push_back("-lc++abi");
4480b57cec5SDimitry Andric     break;
4490b57cec5SDimitry Andric   case ToolChain::CST_Libstdcxx:
450*81ad6265SDimitry Andric     CmdArgs.push_back("-lstdc++");
451*81ad6265SDimitry Andric     break;
4520b57cec5SDimitry Andric   }
4530b57cec5SDimitry Andric }
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric SanitizerMask WebAssembly::getSupportedSanitizers() const {
4560b57cec5SDimitry Andric   SanitizerMask Res = ToolChain::getSupportedSanitizers();
4570b57cec5SDimitry Andric   if (getTriple().isOSEmscripten()) {
4580b57cec5SDimitry Andric     Res |= SanitizerKind::Vptr | SanitizerKind::Leak | SanitizerKind::Address;
4590b57cec5SDimitry Andric   }
4600b57cec5SDimitry Andric   return Res;
4610b57cec5SDimitry Andric }
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric Tool *WebAssembly::buildLinker() const {
4640b57cec5SDimitry Andric   return new tools::wasm::Linker(*this);
4650b57cec5SDimitry Andric }
466*81ad6265SDimitry Andric 
467*81ad6265SDimitry Andric void WebAssembly::addLibCxxIncludePaths(
468*81ad6265SDimitry Andric     const llvm::opt::ArgList &DriverArgs,
469*81ad6265SDimitry Andric     llvm::opt::ArgStringList &CC1Args) const {
470*81ad6265SDimitry Andric   const Driver &D = getDriver();
471*81ad6265SDimitry Andric   std::string SysRoot = computeSysRoot();
472*81ad6265SDimitry Andric   std::string LibPath = SysRoot + "/include";
473*81ad6265SDimitry Andric   const std::string MultiarchTriple =
474*81ad6265SDimitry Andric       getMultiarchTriple(D, getTriple(), SysRoot);
475*81ad6265SDimitry Andric   bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
476*81ad6265SDimitry Andric 
477*81ad6265SDimitry Andric   std::string Version = detectLibcxxVersion(LibPath);
478*81ad6265SDimitry Andric   if (Version.empty())
479*81ad6265SDimitry Andric     return;
480*81ad6265SDimitry Andric 
481*81ad6265SDimitry Andric   // First add the per-target include path if the OS is known.
482*81ad6265SDimitry Andric   if (IsKnownOs) {
483*81ad6265SDimitry Andric     std::string TargetDir = LibPath + "/" + MultiarchTriple + "/c++/" + Version;
484*81ad6265SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, TargetDir);
485*81ad6265SDimitry Andric   }
486*81ad6265SDimitry Andric 
487*81ad6265SDimitry Andric   // Second add the generic one.
488*81ad6265SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
489*81ad6265SDimitry Andric }
490*81ad6265SDimitry Andric 
491*81ad6265SDimitry Andric void WebAssembly::addLibStdCXXIncludePaths(
492*81ad6265SDimitry Andric     const llvm::opt::ArgList &DriverArgs,
493*81ad6265SDimitry Andric     llvm::opt::ArgStringList &CC1Args) const {
494*81ad6265SDimitry Andric   // We cannot use GCCInstallationDetector here as the sysroot usually does
495*81ad6265SDimitry Andric   // not contain a full GCC installation.
496*81ad6265SDimitry Andric   // Instead, we search the given sysroot for /usr/include/xx, similar
497*81ad6265SDimitry Andric   // to how we do it for libc++.
498*81ad6265SDimitry Andric   const Driver &D = getDriver();
499*81ad6265SDimitry Andric   std::string SysRoot = computeSysRoot();
500*81ad6265SDimitry Andric   std::string LibPath = SysRoot + "/include";
501*81ad6265SDimitry Andric   const std::string MultiarchTriple =
502*81ad6265SDimitry Andric       getMultiarchTriple(D, getTriple(), SysRoot);
503*81ad6265SDimitry Andric   bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
504*81ad6265SDimitry Andric 
505*81ad6265SDimitry Andric   // This is similar to detectLibcxxVersion()
506*81ad6265SDimitry Andric   std::string Version;
507*81ad6265SDimitry Andric   {
508*81ad6265SDimitry Andric     std::error_code EC;
509*81ad6265SDimitry Andric     Generic_GCC::GCCVersion MaxVersion =
510*81ad6265SDimitry Andric         Generic_GCC::GCCVersion::Parse("0.0.0");
511*81ad6265SDimitry Andric     SmallString<128> Path(LibPath);
512*81ad6265SDimitry Andric     llvm::sys::path::append(Path, "c++");
513*81ad6265SDimitry Andric     for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Path, EC), LE;
514*81ad6265SDimitry Andric          !EC && LI != LE; LI = LI.increment(EC)) {
515*81ad6265SDimitry Andric       StringRef VersionText = llvm::sys::path::filename(LI->path());
516*81ad6265SDimitry Andric       if (VersionText[0] != 'v') {
517*81ad6265SDimitry Andric         auto Version = Generic_GCC::GCCVersion::Parse(VersionText);
518*81ad6265SDimitry Andric         if (Version > MaxVersion)
519*81ad6265SDimitry Andric           MaxVersion = Version;
520*81ad6265SDimitry Andric       }
521*81ad6265SDimitry Andric     }
522*81ad6265SDimitry Andric     if (MaxVersion.Major > 0)
523*81ad6265SDimitry Andric       Version = MaxVersion.Text;
524*81ad6265SDimitry Andric   }
525*81ad6265SDimitry Andric 
526*81ad6265SDimitry Andric   if (Version.empty())
527*81ad6265SDimitry Andric     return;
528*81ad6265SDimitry Andric 
529*81ad6265SDimitry Andric   // First add the per-target include path if the OS is known.
530*81ad6265SDimitry Andric   if (IsKnownOs) {
531*81ad6265SDimitry Andric     std::string TargetDir = LibPath + "/c++/" + Version + "/" + MultiarchTriple;
532*81ad6265SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, TargetDir);
533*81ad6265SDimitry Andric   }
534*81ad6265SDimitry Andric 
535*81ad6265SDimitry Andric   // Second add the generic one.
536*81ad6265SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
537*81ad6265SDimitry Andric   // Third the backward one.
538*81ad6265SDimitry Andric   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version + "/backward");
539*81ad6265SDimitry Andric }
540