1//===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "WebAssembly.h"
10#include "Gnu.h"
11#include "clang/Config/config.h"
12#include "clang/Driver/CommonArgs.h"
13#include "clang/Driver/Compilation.h"
14#include "clang/Driver/Driver.h"
15#include "clang/Options/Options.h"
16#include "llvm/Config/llvm-config.h" // for LLVM_VERSION_STRING
17#include "llvm/Option/ArgList.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/VirtualFileSystem.h"
21
22using namespace clang::driver;
23using namespace clang::driver::tools;
24using namespace clang::driver::toolchains;
25using namespace clang;
26using namespace llvm::opt;
27
28/// Following the conventions in https://wiki.debian.org/Multiarch/Tuples,
29/// we remove the vendor field to form the multiarch triple.
30std::string WebAssembly::getMultiarchTriple(const Driver &D,
31 const llvm::Triple &TargetTriple,
32 StringRef SysRoot) const {
33 return (TargetTriple.getArchName() + "-" +
34 TargetTriple.getOSAndEnvironmentName()).str();
35}
36
37/// Returns a directory name in which separate objects compile with/without
38/// exceptions may lie. This is used both for `#include` paths as well as lib
39/// paths.
40static std::string GetCXXExceptionsDir(const ArgList &DriverArgs) {
41 if (DriverArgs.getLastArg(Ids: options::OPT_fwasm_exceptions))
42 return "eh";
43 return "noeh";
44}
45
46std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
47 const ToolChain &ToolChain = getToolChain();
48 if (const Arg* A = Args.getLastArg(Ids: options::OPT_fuse_ld_EQ)) {
49 StringRef UseLinker = A->getValue();
50 if (!UseLinker.empty()) {
51 if (llvm::sys::path::is_absolute(path: UseLinker) &&
52 llvm::sys::fs::can_execute(Path: UseLinker))
53 return std::string(UseLinker);
54
55 // Interpret 'lld' as explicitly requesting `wasm-ld`, so look for that
56 // linker. Note that for `wasm32-wasip2` this overrides the default linker
57 // of `wasm-component-ld`.
58 if (UseLinker == "lld") {
59 return ToolChain.GetProgramPath(Name: "wasm-ld");
60 }
61
62 // Allow 'ld' as an alias for the default linker
63 if (UseLinker != "ld")
64 ToolChain.getDriver().Diag(DiagID: diag::err_drv_invalid_linker_name)
65 << A->getAsString(Args);
66 }
67 }
68
69 return ToolChain.GetProgramPath(Name: ToolChain.getDefaultLinker());
70}
71
72static bool TargetBuildsComponents(const llvm::Triple &TargetTriple) {
73 // WASIp2 and above are all based on components, so test for WASI but exclude
74 // the original `wasi` target in addition to the `wasip1` name.
75 return TargetTriple.isOSWASI() && TargetTriple.getOSName() != "wasip1" &&
76 TargetTriple.getOSName() != "wasi";
77}
78
79static bool WantsPthread(const llvm::Triple &Triple, const ArgList &Args) {
80 bool WantsPthread =
81 Args.hasFlag(Pos: options::OPT_pthread, Neg: options::OPT_no_pthread, Default: false);
82
83 // If the WASI environment is "threads" then enable pthreads support
84 // without requiring -pthread, in order to prevent user error
85 if (Triple.isOSWASI() && Triple.getEnvironmentName() == "threads")
86 WantsPthread = true;
87
88 // WASIp3 also implies pthreads support
89 if (Triple.getOS() == llvm::Triple::WASIp3)
90 WantsPthread = true;
91
92 return WantsPthread;
93}
94
95static bool WantsCooperativeMultithreading(const llvm::Triple &Triple,
96 const ArgList &Args) {
97 return Triple.getOS() == llvm::Triple::WASIp3;
98}
99
100static bool WantsSharedMemory(const llvm::Triple &Triple, const ArgList &Args) {
101 return WantsPthread(Triple, Args) &&
102 !WantsCooperativeMultithreading(Triple, Args);
103}
104
105void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
106 const InputInfo &Output,
107 const InputInfoList &Inputs,
108 const ArgList &Args,
109 const char *LinkingOutput) const {
110
111 const ToolChain &ToolChain = getToolChain();
112 const char *Linker = Args.MakeArgString(Str: getLinkerPath(Args));
113 ArgStringList CmdArgs;
114
115 CmdArgs.push_back(Elt: "-m");
116 if (ToolChain.getTriple().isArch64Bit())
117 CmdArgs.push_back(Elt: "wasm64");
118 else
119 CmdArgs.push_back(Elt: "wasm32");
120
121 if (Args.hasArg(Ids: options::OPT_s))
122 CmdArgs.push_back(Elt: "--strip-all");
123
124 // On `wasip2` the default linker is `wasm-component-ld` which wraps the
125 // execution of `wasm-ld`. Find `wasm-ld` and pass it as an argument of where
126 // to find it to avoid it needing to hunt and rediscover or search `PATH` for
127 // where it is.
128 if (llvm::sys::path::stem(path: Linker).ends_with_insensitive(
129 Suffix: "wasm-component-ld")) {
130 CmdArgs.push_back(Elt: "--wasm-ld-path");
131 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetProgramPath(Name: "wasm-ld")));
132 }
133
134 Args.addAllArgs(Output&: CmdArgs, Ids: {options::OPT_L, options::OPT_u});
135
136 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
137
138 bool IsCommand = true;
139 const char *Crt1;
140 const char *Entry = nullptr;
141
142 // When -shared is specified, use the reactor exec model unless
143 // specified otherwise.
144 if (Args.hasArg(Ids: options::OPT_shared))
145 IsCommand = false;
146
147 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mexec_model_EQ)) {
148 StringRef CM = A->getValue();
149 if (CM == "command") {
150 IsCommand = true;
151 } else if (CM == "reactor") {
152 IsCommand = false;
153 } else {
154 ToolChain.getDriver().Diag(DiagID: diag::err_drv_invalid_argument_to_option)
155 << CM << A->getOption().getName();
156 }
157 }
158
159 if (IsCommand) {
160 // If crt1-command.o exists, it supports new-style commands, so use it.
161 // Otherwise, use the old crt1.o. This is a temporary transition measure.
162 // Once WASI libc no longer needs to support LLVM versions which lack
163 // support for new-style command, it can make crt1.o the same as
164 // crt1-command.o. And once LLVM no longer needs to support WASI libc
165 // versions before that, it can switch to using crt1-command.o.
166 Crt1 = "crt1.o";
167 if (ToolChain.GetFilePath(Name: "crt1-command.o") != "crt1-command.o")
168 Crt1 = "crt1-command.o";
169 } else {
170 Crt1 = "crt1-reactor.o";
171 Entry = "_initialize";
172 }
173
174 if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nostartfiles))
175 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: Crt1)));
176 if (Entry) {
177 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--entry"));
178 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Entry));
179 }
180
181 if (Args.hasArg(Ids: options::OPT_shared))
182 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-shared"));
183
184 AddLinkerInputs(TC: ToolChain, Inputs, Args, CmdArgs, JA);
185
186 if (WantsCooperativeMultithreading(Triple: ToolChain.getTriple(), Args))
187 CmdArgs.push_back(Elt: "--cooperative-threading");
188
189 if (WantsSharedMemory(Triple: ToolChain.getTriple(), Args))
190 CmdArgs.push_back(Elt: "--shared-memory");
191
192 if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) {
193 if (ToolChain.ShouldLinkCXXStdlib(Args))
194 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
195
196 if (WantsPthread(Triple: ToolChain.getTriple(), Args))
197 CmdArgs.push_back(Elt: "-lpthread");
198
199 CmdArgs.push_back(Elt: "-lc");
200 AddRunTimeLibs(TC: ToolChain, D: ToolChain.getDriver(), CmdArgs, Args);
201 }
202
203 ToolChain.addProfileRTLibs(Args, CmdArgs);
204
205 CmdArgs.push_back(Elt: "-o");
206 CmdArgs.push_back(Elt: Output.getFilename());
207
208 // Don't use wasm-opt by default on `wasip2` as it doesn't have support for
209 // components at this time. Retain the historical default otherwise, though,
210 // of running `wasm-opt` by default.
211 bool WasmOptDefault = !TargetBuildsComponents(TargetTriple: ToolChain.getTriple());
212 bool RunWasmOpt = Args.hasFlag(Pos: options::OPT_wasm_opt,
213 Neg: options::OPT_no_wasm_opt, Default: WasmOptDefault);
214
215 // If wasm-opt is enabled and optimizations are happening look for the
216 // `wasm-opt` program. If it's not found auto-disable it.
217 std::string WasmOptPath;
218 if (RunWasmOpt && Args.getLastArg(Ids: options::OPT_O_Group)) {
219 WasmOptPath = ToolChain.GetProgramPath(Name: "wasm-opt");
220 if (WasmOptPath == "wasm-opt") {
221 WasmOptPath = {};
222 }
223 }
224
225 if (!WasmOptPath.empty()) {
226 CmdArgs.push_back(Elt: "--keep-section=target_features");
227 }
228
229 C.addCommand(Cmd: std::make_unique<Command>(args: JA, args: *this,
230 args: ResponseFileSupport::AtFileCurCP(),
231 args&: Linker, args&: CmdArgs, args: Inputs, args: Output));
232
233 if (Arg *A = Args.getLastArg(Ids: options::OPT_O_Group)) {
234 if (!WasmOptPath.empty()) {
235 StringRef OOpt = "s";
236 if (A->getOption().matches(ID: options::OPT_O4) ||
237 A->getOption().matches(ID: options::OPT_Ofast))
238 OOpt = "4";
239 else if (A->getOption().matches(ID: options::OPT_O0))
240 OOpt = "0";
241 else if (A->getOption().matches(ID: options::OPT_O))
242 OOpt = A->getValue();
243
244 if (OOpt != "0") {
245 const char *WasmOpt = Args.MakeArgString(Str: WasmOptPath);
246 ArgStringList OptArgs;
247 OptArgs.push_back(Elt: Output.getFilename());
248 OptArgs.push_back(Elt: Args.MakeArgString(Str: llvm::Twine("-O") + OOpt));
249 OptArgs.push_back(Elt: "-o");
250 OptArgs.push_back(Elt: Output.getFilename());
251 C.addCommand(Cmd: std::make_unique<Command>(
252 args: JA, args: *this, args: ResponseFileSupport::AtFileCurCP(), args&: WasmOpt, args&: OptArgs,
253 args: Inputs, args: Output));
254 }
255 }
256 }
257}
258
259/// Append `Dir` to `Paths`, but also include the LTO directories before that if
260/// LTO is enabled.
261static void AppendLibDirAndLTODir(ToolChain::path_list &Paths,
262 const ToolChain &TC,
263 const llvm::opt::ArgList &Args,
264 const std::string &Dir) {
265 if (TC.isUsingLTO(Args)) {
266 // The version allows the path to be keyed to the specific version of
267 // LLVM in used, as the bitcode format is not stable.
268 Paths.push_back(Elt: Dir + "/llvm-lto/" LLVM_VERSION_STRING);
269 }
270 Paths.push_back(Elt: Dir);
271}
272
273WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
274 const llvm::opt::ArgList &Args)
275 : ToolChain(D, Triple, Args) {
276
277 assert(Triple.isArch32Bit() != Triple.isArch64Bit());
278
279 getProgramPaths().push_back(Elt: getDriver().Dir);
280
281 auto SysRoot = getDriver().SysRoot;
282 if (getTriple().getOS() == llvm::Triple::UnknownOS) {
283 // Theoretically an "unknown" OS should mean no standard libraries, however
284 // it could also mean that a custom set of libraries is in use, so just add
285 // /lib to the search path. Disable multiarch in this case, to discourage
286 // paths containing "unknown" from acquiring meanings.
287 getFilePaths().push_back(Elt: SysRoot + "/lib");
288 } else {
289 const std::string MultiarchTriple =
290 getMultiarchTriple(D: getDriver(), TargetTriple: Triple, SysRoot);
291 std::string TripleLibDir = SysRoot + "/lib/" + MultiarchTriple;
292 // Allow sysroots to segregate objects based on whether exceptions are
293 // enabled or not. This is intended to assist with distribution of pre-built
294 // sysroots that contain libraries that are capable of producing binaries
295 // entirely without exception-handling instructions but also with if
296 // exceptions are enabled, for example.
297 AppendLibDirAndLTODir(Paths&: getFilePaths(), TC: *this, Args,
298 Dir: TripleLibDir + "/" + GetCXXExceptionsDir(DriverArgs: Args));
299 AppendLibDirAndLTODir(Paths&: getFilePaths(), TC: *this, Args, Dir: TripleLibDir);
300 }
301
302 if (getTriple().getOS() == llvm::Triple::WASI) {
303 D.Diag(DiagID: diag::warn_drv_deprecated_custom)
304 << "--target=wasm32-wasi"
305 << "use --target=wasm32-wasip1 instead";
306 }
307}
308
309const char *WebAssembly::getDefaultLinker() const {
310 if (TargetBuildsComponents(TargetTriple: getTriple()))
311 return "wasm-component-ld";
312 return "wasm-ld";
313}
314
315bool WebAssembly::IsMathErrnoDefault() const { return false; }
316
317bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
318
319bool WebAssembly::UseObjCMixedDispatch() const { return true; }
320
321bool WebAssembly::isPICDefault() const { return false; }
322
323bool WebAssembly::isPIEDefault(const llvm::opt::ArgList &Args) const {
324 return false;
325}
326
327bool WebAssembly::isPICDefaultForced() const { return false; }
328
329bool WebAssembly::hasBlocksRuntime() const { return false; }
330
331// TODO: Support profiling.
332bool WebAssembly::SupportsProfiling() const { return false; }
333
334bool WebAssembly::HasNativeLLVMSupport() const { return true; }
335
336void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
337 ArgStringList &CC1Args, BoundArch BA,
338 Action::OffloadKind) const {
339 if (!DriverArgs.hasFlag(Pos: options::OPT_fuse_init_array,
340 Neg: options::OPT_fno_use_init_array, Default: true))
341 CC1Args.push_back(Elt: "-fno-use-init-array");
342
343 // '-pthread' implies bulk-memory, mutable-globals, and sign-ext.
344 // It also implies atomics, so long as we're not targeting a cooperative
345 // threading environment.
346 if (WantsPthread(Triple: getTriple(), Args: DriverArgs)) {
347 if (!WantsCooperativeMultithreading(Triple: getTriple(), Args: DriverArgs) &&
348 DriverArgs.hasFlag(Pos: options::OPT_mno_atomics, Neg: options::OPT_matomics,
349 Default: false))
350 getDriver().Diag(DiagID: diag::err_drv_argument_not_allowed_with)
351 << "-pthread"
352 << "-mno-atomics";
353 if (DriverArgs.hasFlag(Pos: options::OPT_mno_bulk_memory,
354 Neg: options::OPT_mbulk_memory, Default: false))
355 getDriver().Diag(DiagID: diag::err_drv_argument_not_allowed_with)
356 << "-pthread"
357 << "-mno-bulk-memory";
358 if (DriverArgs.hasFlag(Pos: options::OPT_mno_mutable_globals,
359 Neg: options::OPT_mmutable_globals, Default: false))
360 getDriver().Diag(DiagID: diag::err_drv_argument_not_allowed_with)
361 << "-pthread"
362 << "-mno-mutable-globals";
363 if (DriverArgs.hasFlag(Pos: options::OPT_mno_sign_ext, Neg: options::OPT_msign_ext,
364 Default: false))
365 getDriver().Diag(DiagID: diag::err_drv_argument_not_allowed_with)
366 << "-pthread"
367 << "-mno-sign-ext";
368 if (!WantsCooperativeMultithreading(Triple: getTriple(), Args: DriverArgs)) {
369 CC1Args.push_back(Elt: "-target-feature");
370 CC1Args.push_back(Elt: "+atomics");
371 }
372 CC1Args.push_back(Elt: "-target-feature");
373 CC1Args.push_back(Elt: "+bulk-memory");
374 CC1Args.push_back(Elt: "-target-feature");
375 CC1Args.push_back(Elt: "+mutable-globals");
376 CC1Args.push_back(Elt: "-target-feature");
377 CC1Args.push_back(Elt: "+sign-ext");
378 }
379
380 if (!DriverArgs.hasFlag(Pos: options::OPT_mmutable_globals,
381 Neg: options::OPT_mno_mutable_globals, Default: false)) {
382 // -fPIC implies +mutable-globals because the PIC ABI used by the linker
383 // depends on importing and exporting mutable globals.
384 llvm::Reloc::Model RelocationModel;
385 unsigned PICLevel;
386 bool IsPIE;
387 std::tie(args&: RelocationModel, args&: PICLevel, args&: IsPIE) =
388 ParsePICArgs(ToolChain: *this, Args: DriverArgs);
389 if (RelocationModel == llvm::Reloc::PIC_) {
390 if (DriverArgs.hasFlag(Pos: options::OPT_mno_mutable_globals,
391 Neg: options::OPT_mmutable_globals, Default: false)) {
392 getDriver().Diag(DiagID: diag::err_drv_argument_not_allowed_with)
393 << "-fPIC"
394 << "-mno-mutable-globals";
395 }
396 CC1Args.push_back(Elt: "-target-feature");
397 CC1Args.push_back(Elt: "+mutable-globals");
398 }
399 }
400
401 bool HasBannedIncompatibleOptionsForWasmEHSjLj = false;
402 bool HasEnabledFeaturesForWasmEHSjLj = false;
403
404 // Bans incompatible options for Wasm EH / SjLj. We don't allow using
405 // different modes for EH and SjLj.
406 auto BanIncompatibleOptionsForWasmEHSjLj = [&](StringRef CurOption) {
407 if (HasBannedIncompatibleOptionsForWasmEHSjLj)
408 return;
409 HasBannedIncompatibleOptionsForWasmEHSjLj = true;
410 if (DriverArgs.hasFlag(Pos: options::OPT_mno_exception_handing,
411 Neg: options::OPT_mexception_handing, Default: false))
412 getDriver().Diag(DiagID: diag::err_drv_argument_not_allowed_with)
413 << CurOption << "-mno-exception-handling";
414 // The standardized Wasm EH spec requires multivalue and reference-types.
415 if (DriverArgs.hasFlag(Pos: options::OPT_mno_multivalue,
416 Neg: options::OPT_mmultivalue, Default: false))
417 getDriver().Diag(DiagID: diag::err_drv_argument_not_allowed_with)
418 << CurOption << "-mno-multivalue";
419 if (DriverArgs.hasFlag(Pos: options::OPT_mno_reference_types,
420 Neg: options::OPT_mreference_types, Default: false))
421 getDriver().Diag(DiagID: diag::err_drv_argument_not_allowed_with)
422 << CurOption << "-mno-reference-types";
423
424 for (const Arg *A : DriverArgs.filtered(Ids: options::OPT_mllvm)) {
425 for (const auto *Option :
426 {"-enable-emscripten-cxx-exceptions", "-enable-emscripten-sjlj",
427 "-emscripten-cxx-exceptions-allowed"}) {
428 if (StringRef(A->getValue(N: 0)) == Option)
429 getDriver().Diag(DiagID: diag::err_drv_argument_not_allowed_with)
430 << CurOption << Option;
431 }
432 }
433 };
434
435 // Enable necessary features for Wasm EH / SjLj in the backend.
436 auto EnableFeaturesForWasmEHSjLj = [&]() {
437 if (HasEnabledFeaturesForWasmEHSjLj)
438 return;
439 HasEnabledFeaturesForWasmEHSjLj = true;
440 CC1Args.push_back(Elt: "-target-feature");
441 CC1Args.push_back(Elt: "+exception-handling");
442 // The standardized Wasm EH spec requires multivalue and reference-types.
443 CC1Args.push_back(Elt: "-target-feature");
444 CC1Args.push_back(Elt: "+multivalue");
445 CC1Args.push_back(Elt: "-target-feature");
446 CC1Args.push_back(Elt: "+reference-types");
447 // Backend needs '-exception-model=wasm' to use Wasm EH instructions
448 CC1Args.push_back(Elt: "-exception-model=wasm");
449 };
450
451 if (DriverArgs.getLastArg(Ids: options::OPT_fwasm_exceptions)) {
452 BanIncompatibleOptionsForWasmEHSjLj("-fwasm-exceptions");
453 EnableFeaturesForWasmEHSjLj();
454 // Backend needs -wasm-enable-eh to enable Wasm EH
455 CC1Args.push_back(Elt: "-mllvm");
456 CC1Args.push_back(Elt: "-wasm-enable-eh");
457 }
458
459 for (const Arg *A : DriverArgs.filtered(Ids: options::OPT_mllvm)) {
460 StringRef Opt = A->getValue(N: 0);
461 if (Opt.starts_with(Prefix: "-emscripten-cxx-exceptions-allowed")) {
462 // '-mllvm -emscripten-cxx-exceptions-allowed' should be used with
463 // '-mllvm -enable-emscripten-cxx-exceptions'
464 bool EmEHArgExists = false;
465 for (const Arg *A : DriverArgs.filtered(Ids: options::OPT_mllvm)) {
466 if (StringRef(A->getValue(N: 0)) == "-enable-emscripten-cxx-exceptions") {
467 EmEHArgExists = true;
468 break;
469 }
470 }
471 if (!EmEHArgExists)
472 getDriver().Diag(DiagID: diag::err_drv_argument_only_allowed_with)
473 << "-mllvm -emscripten-cxx-exceptions-allowed"
474 << "-mllvm -enable-emscripten-cxx-exceptions";
475
476 // Prevent functions specified in -emscripten-cxx-exceptions-allowed list
477 // from being inlined before reaching the wasm backend.
478 StringRef FuncNamesStr = Opt.split(Separator: '=').second;
479 SmallVector<StringRef, 4> FuncNames;
480 FuncNamesStr.split(A&: FuncNames, Separator: ',');
481 for (auto Name : FuncNames) {
482 CC1Args.push_back(Elt: "-mllvm");
483 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: "--force-attribute=" + Name +
484 ":noinline"));
485 }
486 }
487
488 for (const auto *Option :
489 {"-wasm-enable-eh", "-wasm-enable-sjlj", "-wasm-use-legacy-eh"}) {
490 if (Opt.starts_with(Prefix: Option)) {
491 BanIncompatibleOptionsForWasmEHSjLj(Option);
492 EnableFeaturesForWasmEHSjLj();
493 }
494 }
495 }
496}
497
498ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
499 return ToolChain::RLT_CompilerRT;
500}
501
502ToolChain::CXXStdlibType
503WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
504 if (Arg *A = Args.getLastArg(Ids: options::OPT_stdlib_EQ)) {
505 StringRef Value = A->getValue();
506 if (Value == "libc++")
507 return ToolChain::CST_Libcxx;
508 else if (Value == "libstdc++")
509 return ToolChain::CST_Libstdcxx;
510 else
511 getDriver().Diag(DiagID: diag::err_drv_invalid_stdlib_name)
512 << A->getAsString(Args);
513 }
514 return ToolChain::CST_Libcxx;
515}
516
517void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
518 ArgStringList &CC1Args) const {
519 if (DriverArgs.hasArg(Ids: options::OPT_nostdinc))
520 return;
521
522 const Driver &D = getDriver();
523
524 if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) {
525 SmallString<128> P(D.ResourceDir);
526 llvm::sys::path::append(path&: P, a: "include");
527 addSystemInclude(DriverArgs, CC1Args, Path: P);
528 }
529
530 if (DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))
531 return;
532
533 // Check for configure-time C include directories.
534 StringRef CIncludeDirs(C_INCLUDE_DIRS);
535 if (CIncludeDirs != "") {
536 SmallVector<StringRef, 5> dirs;
537 CIncludeDirs.split(A&: dirs, Separator: ":");
538 for (StringRef dir : dirs) {
539 StringRef Prefix =
540 llvm::sys::path::is_absolute(path: dir) ? "" : StringRef(D.SysRoot);
541 addExternCSystemInclude(DriverArgs, CC1Args, Path: Prefix + dir);
542 }
543 return;
544 }
545
546 if (getTriple().getOS() != llvm::Triple::UnknownOS) {
547 const std::string MultiarchTriple =
548 getMultiarchTriple(D, TargetTriple: getTriple(), SysRoot: D.SysRoot);
549 addSystemInclude(DriverArgs, CC1Args, Path: D.SysRoot + "/include/" + MultiarchTriple);
550 }
551 addSystemInclude(DriverArgs, CC1Args, Path: D.SysRoot + "/include");
552}
553
554void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
555 ArgStringList &CC1Args) const {
556
557 if (DriverArgs.hasArg(Ids: options::OPT_nostdlibinc, Ids: options::OPT_nostdinc,
558 Ids: options::OPT_nostdincxx))
559 return;
560
561 switch (GetCXXStdlibType(Args: DriverArgs)) {
562 case ToolChain::CST_Libcxx:
563 addLibCxxIncludePaths(DriverArgs, CC1Args);
564 break;
565 case ToolChain::CST_Libstdcxx:
566 addLibStdCXXIncludePaths(DriverArgs, CC1Args);
567 break;
568 }
569}
570
571void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
572 llvm::opt::ArgStringList &CmdArgs) const {
573
574 switch (GetCXXStdlibType(Args)) {
575 case ToolChain::CST_Libcxx:
576 CmdArgs.push_back(Elt: "-lc++");
577 if (Args.hasArg(Ids: options::OPT_fexperimental_library))
578 CmdArgs.push_back(Elt: "-lc++experimental");
579 CmdArgs.push_back(Elt: "-lc++abi");
580 break;
581 case ToolChain::CST_Libstdcxx:
582 CmdArgs.push_back(Elt: "-lstdc++");
583 break;
584 }
585}
586
587SanitizerMask WebAssembly::getSupportedSanitizers(
588 BoundArch BA, Action::OffloadKind DeviceOffloadKind) const {
589 SanitizerMask Res = ToolChain::getSupportedSanitizers(BA, DeviceOffloadKind);
590 if (getTriple().isOSEmscripten()) {
591 Res |= SanitizerKind::Vptr | SanitizerKind::Leak;
592 }
593
594 if (getTriple().isOSEmscripten() || getTriple().isOSWASI()) {
595 Res |= SanitizerKind::Address;
596 }
597
598 // -fsanitize=function places two words before the function label, which are
599 // -unsupported.
600 Res &= ~SanitizerKind::Function;
601 return Res;
602}
603
604Tool *WebAssembly::buildLinker() const {
605 return new tools::wasm::Linker(*this);
606}
607
608void WebAssembly::addLibCxxIncludePaths(
609 const llvm::opt::ArgList &DriverArgs,
610 llvm::opt::ArgStringList &CC1Args) const {
611 const Driver &D = getDriver();
612 std::string SysRoot = computeSysRoot();
613 std::string LibPath = SysRoot + "/include";
614 const std::string MultiarchTriple =
615 getMultiarchTriple(D, TargetTriple: getTriple(), SysRoot);
616 bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
617
618 std::string Version = detectLibcxxVersion(IncludePath: LibPath);
619 if (Version.empty())
620 return;
621
622 // First add the per-target-per-exception-handling include path if the
623 // OS is known, then second add the per-target include path.
624 if (IsKnownOs) {
625 std::string TargetDir = LibPath + "/" + MultiarchTriple;
626 std::string Suffix = "/c++/" + Version;
627 addSystemInclude(DriverArgs, CC1Args,
628 Path: TargetDir + "/" + GetCXXExceptionsDir(DriverArgs) +
629 Suffix);
630 addSystemInclude(DriverArgs, CC1Args, Path: TargetDir + Suffix);
631 }
632
633 // Third add the generic one.
634 addSystemInclude(DriverArgs, CC1Args, Path: LibPath + "/c++/" + Version);
635}
636
637void WebAssembly::addLibStdCXXIncludePaths(
638 const llvm::opt::ArgList &DriverArgs,
639 llvm::opt::ArgStringList &CC1Args) const {
640 // We cannot use GCCInstallationDetector here as the sysroot usually does
641 // not contain a full GCC installation.
642 // Instead, we search the given sysroot for /usr/include/xx, similar
643 // to how we do it for libc++.
644 const Driver &D = getDriver();
645 std::string SysRoot = computeSysRoot();
646 std::string LibPath = SysRoot + "/include";
647 const std::string MultiarchTriple =
648 getMultiarchTriple(D, TargetTriple: getTriple(), SysRoot);
649 bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
650
651 // This is similar to detectLibcxxVersion()
652 std::string Version;
653 {
654 std::error_code EC;
655 Generic_GCC::GCCVersion MaxVersion =
656 Generic_GCC::GCCVersion::Parse(VersionText: "0.0.0");
657 SmallString<128> Path(LibPath);
658 llvm::sys::path::append(path&: Path, a: "c++");
659 for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Dir: Path, EC), LE;
660 !EC && LI != LE; LI = LI.increment(EC)) {
661 StringRef VersionText = llvm::sys::path::filename(path: LI->path());
662 if (VersionText[0] != 'v') {
663 auto Version = Generic_GCC::GCCVersion::Parse(VersionText);
664 if (Version > MaxVersion)
665 MaxVersion = Version;
666 }
667 }
668 if (MaxVersion.Major > 0)
669 Version = MaxVersion.Text;
670 }
671
672 if (Version.empty())
673 return;
674
675 // First add the per-target include path if the OS is known.
676 if (IsKnownOs) {
677 std::string TargetDir = LibPath + "/c++/" + Version + "/" + MultiarchTriple;
678 addSystemInclude(DriverArgs, CC1Args, Path: TargetDir);
679 }
680
681 // Second add the generic one.
682 addSystemInclude(DriverArgs, CC1Args, Path: LibPath + "/c++/" + Version);
683 // Third the backward one.
684 addSystemInclude(DriverArgs, CC1Args, Path: LibPath + "/c++/" + Version + "/backward");
685}
686