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