clang 23.0.0git
WebAssembly.cpp
Go to the documentation of this file.
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"
14#include "clang/Driver/Driver.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(options::OPT_fwasm_exceptions))
42 return "eh";
43 return "noeh";
44}
45
46std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
48 if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
49 StringRef UseLinker = A->getValue();
50 if (!UseLinker.empty()) {
51 if (llvm::sys::path::is_absolute(UseLinker) &&
52 llvm::sys::fs::can_execute(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("wasm-ld");
60 }
61
62 // Allow 'ld' as an alias for the default linker
63 if (UseLinker != "ld")
64 ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
65 << A->getAsString(Args);
66 }
67 }
68
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(options::OPT_pthread, options::OPT_no_pthread, 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
92 const InputInfo &Output,
93 const InputInfoList &Inputs,
94 const ArgList &Args,
95 const char *LinkingOutput) const {
96
98 const char *Linker = Args.MakeArgString(getLinkerPath(Args));
99 ArgStringList CmdArgs;
100
101 CmdArgs.push_back("-m");
102 if (ToolChain.getTriple().isArch64Bit())
103 CmdArgs.push_back("wasm64");
104 else
105 CmdArgs.push_back("wasm32");
106
107 if (Args.hasArg(options::OPT_s))
108 CmdArgs.push_back("--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(Linker).ends_with_insensitive(
115 "wasm-component-ld")) {
116 CmdArgs.push_back("--wasm-ld-path");
117 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetProgramPath("wasm-ld")));
118 }
119
120 Args.addAllArgs(CmdArgs, {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(options::OPT_shared))
131 IsCommand = false;
132
133 if (const Arg *A = Args.getLastArg(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(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("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(options::OPT_nostdlib, options::OPT_nostartfiles))
161 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
162 if (Entry) {
163 CmdArgs.push_back(Args.MakeArgString("--entry"));
164 CmdArgs.push_back(Args.MakeArgString(Entry));
165 }
166
167 if (Args.hasArg(options::OPT_shared))
168 CmdArgs.push_back(Args.MakeArgString("-shared"));
169
170 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
171
172 if (WantsPthread(ToolChain.getTriple(), Args))
173 CmdArgs.push_back("--shared-memory");
174
175 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
177 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
178
179 if (WantsPthread(ToolChain.getTriple(), Args))
180 CmdArgs.push_back("-lpthread");
181
182 CmdArgs.push_back("-lc");
183 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
184 }
185
186 ToolChain.addProfileRTLibs(Args, CmdArgs);
187
188 CmdArgs.push_back("-o");
189 CmdArgs.push_back(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(ToolChain.getTriple());
195 bool RunWasmOpt = Args.hasFlag(options::OPT_wasm_opt,
196 options::OPT_no_wasm_opt, 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(options::OPT_O_Group)) {
202 WasmOptPath = ToolChain.GetProgramPath("wasm-opt");
203 if (WasmOptPath == "wasm-opt") {
204 WasmOptPath = {};
205 }
206 }
207
208 if (!WasmOptPath.empty()) {
209 CmdArgs.push_back("--keep-section=target_features");
210 }
211
212 C.addCommand(std::make_unique<Command>(JA, *this,
214 Linker, CmdArgs, Inputs, Output));
215
216 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
217 if (!WasmOptPath.empty()) {
218 StringRef OOpt = "s";
219 if (A->getOption().matches(options::OPT_O4) ||
220 A->getOption().matches(options::OPT_Ofast))
221 OOpt = "4";
222 else if (A->getOption().matches(options::OPT_O0))
223 OOpt = "0";
224 else if (A->getOption().matches(options::OPT_O))
225 OOpt = A->getValue();
226
227 if (OOpt != "0") {
228 const char *WasmOpt = Args.MakeArgString(WasmOptPath);
229 ArgStringList OptArgs;
230 OptArgs.push_back(Output.getFilename());
231 OptArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
232 OptArgs.push_back("-o");
233 OptArgs.push_back(Output.getFilename());
234 C.addCommand(std::make_unique<Command>(
235 JA, *this, ResponseFileSupport::AtFileCurCP(), WasmOpt, OptArgs,
236 Inputs, Output));
237 }
238 }
239 }
240}
241
242/// Append `Dir` to `Paths`, but also include the LTO directories before that if
243/// LTO is eanbled.
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(Dir + "/llvm-lto/" LLVM_VERSION_STRING);
250 }
251 Paths.push_back(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(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(SysRoot + "/lib");
269 } else {
270 const std::string MultiarchTriple =
271 getMultiarchTriple(getDriver(), 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(getFilePaths(), D,
279 TripleLibDir + "/" + GetCXXExceptionsDir(Args));
280 AppendLibDirAndLTODir(getFilePaths(), D, TripleLibDir);
281 }
282
283 if (getTriple().getOS() == llvm::Triple::WASI) {
284 D.Diag(diag::warn_drv_deprecated_custom)
285 << "--target=wasm32-wasi"
286 << "use --target=wasm32-wasip1 instead";
287 }
288}
289
290const char *WebAssembly::getDefaultLinker() const {
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(options::OPT_fuse_init_array,
321 options::OPT_fno_use_init_array, true))
322 CC1Args.push_back("-fno-use-init-array");
323
324 // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
325 if (WantsPthread(getTriple(), DriverArgs)) {
326 if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
327 false))
328 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
329 << "-pthread"
330 << "-mno-atomics";
331 if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
332 options::OPT_mbulk_memory, false))
333 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
334 << "-pthread"
335 << "-mno-bulk-memory";
336 if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
337 options::OPT_mmutable_globals, false))
338 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
339 << "-pthread"
340 << "-mno-mutable-globals";
341 if (DriverArgs.hasFlag(options::OPT_mno_sign_ext, options::OPT_msign_ext,
342 false))
343 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
344 << "-pthread"
345 << "-mno-sign-ext";
346 CC1Args.push_back("-target-feature");
347 CC1Args.push_back("+atomics");
348 CC1Args.push_back("-target-feature");
349 CC1Args.push_back("+bulk-memory");
350 CC1Args.push_back("-target-feature");
351 CC1Args.push_back("+mutable-globals");
352 CC1Args.push_back("-target-feature");
353 CC1Args.push_back("+sign-ext");
354 }
355
356 if (!DriverArgs.hasFlag(options::OPT_mmutable_globals,
357 options::OPT_mno_mutable_globals, 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(RelocationModel, PICLevel, IsPIE) =
364 ParsePICArgs(*this, DriverArgs);
365 if (RelocationModel == llvm::Reloc::PIC_) {
366 if (DriverArgs.hasFlag(options::OPT_mno_mutable_globals,
367 options::OPT_mmutable_globals, false)) {
368 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
369 << "-fPIC"
370 << "-mno-mutable-globals";
371 }
372 CC1Args.push_back("-target-feature");
373 CC1Args.push_back("+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(options::OPT_mno_exception_handing,
387 options::OPT_mexception_handing, false))
388 getDriver().Diag(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(options::OPT_mno_multivalue,
392 options::OPT_mmultivalue, false))
393 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
394 << CurOption << "-mno-multivalue";
395 if (DriverArgs.hasFlag(options::OPT_mno_reference_types,
396 options::OPT_mreference_types, false))
397 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
398 << CurOption << "-mno-reference-types";
399
400 for (const Arg *A : DriverArgs.filtered(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(0)) == Option)
405 getDriver().Diag(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("-target-feature");
417 CC1Args.push_back("+exception-handling");
418 // The standardized Wasm EH spec requires multivalue and reference-types.
419 CC1Args.push_back("-target-feature");
420 CC1Args.push_back("+multivalue");
421 CC1Args.push_back("-target-feature");
422 CC1Args.push_back("+reference-types");
423 // Backend needs '-exception-model=wasm' to use Wasm EH instructions
424 CC1Args.push_back("-exception-model=wasm");
425 };
426
427 if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
428 BanIncompatibleOptionsForWasmEHSjLj("-fwasm-exceptions");
429 EnableFeaturesForWasmEHSjLj();
430 // Backend needs -wasm-enable-eh to enable Wasm EH
431 CC1Args.push_back("-mllvm");
432 CC1Args.push_back("-wasm-enable-eh");
433 }
434
435 for (const Arg *A : DriverArgs.filtered(options::OPT_mllvm)) {
436 StringRef Opt = A->getValue(0);
437 if (Opt.starts_with("-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(options::OPT_mllvm)) {
442 if (StringRef(A->getValue(0)) == "-enable-emscripten-cxx-exceptions") {
443 EmEHArgExists = true;
444 break;
445 }
446 }
447 if (!EmEHArgExists)
448 getDriver().Diag(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('=').second;
455 SmallVector<StringRef, 4> FuncNames;
456 FuncNamesStr.split(FuncNames, ',');
457 for (auto Name : FuncNames) {
458 CC1Args.push_back("-mllvm");
459 CC1Args.push_back(DriverArgs.MakeArgString("--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(Option)) {
467 BanIncompatibleOptionsForWasmEHSjLj(Option);
468 EnableFeaturesForWasmEHSjLj();
469 }
470 }
471 }
472}
473
476}
477
479WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
480 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
481 StringRef Value = A->getValue();
482 if (Value == "libc++")
484 else if (Value == "libstdc++")
486 else
487 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
488 << A->getAsString(Args);
489 }
491}
492
493void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
494 ArgStringList &CC1Args) const {
495 if (DriverArgs.hasArg(options::OPT_nostdinc))
496 return;
497
498 const Driver &D = getDriver();
499
500 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
501 SmallString<128> P(D.ResourceDir);
502 llvm::sys::path::append(P, "include");
503 addSystemInclude(DriverArgs, CC1Args, P);
504 }
505
506 if (DriverArgs.hasArg(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(dirs, ":");
514 for (StringRef dir : dirs) {
515 StringRef Prefix =
516 llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
517 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
518 }
519 return;
520 }
521
522 if (getTriple().getOS() != llvm::Triple::UnknownOS) {
523 const std::string MultiarchTriple =
524 getMultiarchTriple(D, getTriple(), D.SysRoot);
525 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
526 }
527 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
528}
529
530void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
531 ArgStringList &CC1Args) const {
532
533 if (DriverArgs.hasArg(options::OPT_nostdlibinc, options::OPT_nostdinc,
534 options::OPT_nostdincxx))
535 return;
536
537 switch (GetCXXStdlibType(DriverArgs)) {
539 addLibCxxIncludePaths(DriverArgs, CC1Args);
540 break;
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)) {
552 CmdArgs.push_back("-lc++");
553 if (Args.hasArg(options::OPT_fexperimental_library))
554 CmdArgs.push_back("-lc++experimental");
555 CmdArgs.push_back("-lc++abi");
556 break;
558 CmdArgs.push_back("-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, getTriple(), SysRoot);
591 bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
592
593 std::string Version = detectLibcxxVersion(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 TargetDir + "/" + GetCXXExceptionsDir(DriverArgs) +
604 Suffix);
605 addSystemInclude(DriverArgs, CC1Args, TargetDir + Suffix);
606 }
607
608 // Third add the generic one.
609 addSystemInclude(DriverArgs, CC1Args, 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, 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 =
632 SmallString<128> Path(LibPath);
633 llvm::sys::path::append(Path, "c++");
634 for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Path, EC), LE;
635 !EC && LI != LE; LI = LI.increment(EC)) {
636 StringRef VersionText = llvm::sys::path::filename(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, TargetDir);
654 }
655
656 // Second add the generic one.
657 addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
658 // Third the backward one.
659 addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version + "/backward");
660}
static std::string GetCXXExceptionsDir(const ArgList &DriverArgs)
Returns a directory name in which separate objects compile with/without exceptions may lie.
static bool WantsPthread(const llvm::Triple &Triple, const ArgList &Args)
static void AppendLibDirAndLTODir(ToolChain::path_list &Paths, const Driver &D, const std::string &Dir)
Append Dir to Paths, but also include the LTO directories before that if LTO is eanbled.
static bool TargetBuildsComponents(const llvm::Triple &TargetTriple)
static StringRef getTriple(const Command &Job)
Compilation - A set of tasks to perform for a single driver invocation.
Definition Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:99
DiagnosticBuilder Diag(unsigned DiagID) const
Definition Driver.h:169
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition Driver.h:744
InputInfo - Wrapper for information about an input source.
Definition InputInfo.h:22
const char * getFilename() const
Definition InputInfo.h:83
ToolChain - Access to tools for a single platform.
Definition ToolChain.h:92
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
virtual std::string computeSysRoot() const
Return the sysroot, possibly searching for a default sysroot using target-specific logic.
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments.
std::string GetFilePath(const char *Name) const
StringRef getOS() const
Definition ToolChain.h:280
virtual bool IsObjCNonFragileABIDefault() const
IsObjCNonFragileABIDefault - Does this tool chain set -fobjc-nonfragile-abi by default.
Definition ToolChain.h:476
const Driver & getDriver() const
Definition ToolChain.h:261
virtual std::string detectLibcxxVersion(StringRef IncludePath) const
llvm::vfs::FileSystem & getVFS() const
virtual bool isPICDefaultForced() const =0
Tests whether this toolchain forces its default for PIC, PIE or non-PIC.
virtual bool IsMathErrnoDefault() const
IsMathErrnoDefault - Does this tool chain use -fmath-errno by default.
Definition ToolChain.h:468
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
virtual const char * getDefaultLinker() const
GetDefaultLinker - Get the default linker to use.
Definition ToolChain.h:502
virtual Tool * buildLinker() const
const llvm::Triple & getTriple() const
Definition ToolChain.h:263
virtual bool HasNativeLLVMSupport() const
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
virtual void addProfileRTLibs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass a suitable profile runtime ...
std::string GetProgramPath(const char *Name) const
virtual bool hasBlocksRuntime() const
hasBlocksRuntime - Given that the user is compiling with -fblocks, does this tool chain guarantee the...
Definition ToolChain.h:693
virtual bool SupportsProfiling() const
SupportsProfiling - Does this tool chain support -pg.
Definition ToolChain.h:594
virtual bool UseObjCMixedDispatch() const
UseObjCMixedDispatchDefault - When using non-legacy dispatch, should the mixed dispatch method be use...
Definition ToolChain.h:480
virtual void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
virtual RuntimeLibType GetDefaultRuntimeLibType() const
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition ToolChain.h:505
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
virtual bool isPICDefault() const =0
Test whether this toolchain defaults to PIC.
SmallVector< std::string, 16 > path_list
Definition ToolChain.h:94
const ToolChain & getToolChain() const
Definition Tool.h:52
WebAssembly(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
std::string getLinkerPath(const llvm::opt::ArgList &Args) const
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
std::tuple< llvm::Reloc::Model, unsigned, bool > ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args)
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
SmallVector< InputInfo, 4 > InputInfoList
Definition Driver.h:50
bool LE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1341
The JSON file list parser is used to communicate input to InstallAPI.
bool(*)(llvm::ArrayRef< const char * >, llvm::raw_ostream &, llvm::raw_ostream &, bool, bool) Driver
Definition Wasm.cpp:36
static constexpr ResponseFileSupport AtFileCurCP()
Definition Job.h:92
int Major
The parsed major, minor, and patch numbers.
Definition Gnu.h:168
std::string Text
The unparsed text of the version.
Definition Gnu.h:165
static GCCVersion Parse(StringRef VersionText)
Parse a GCCVersion object out of a string of text.
Definition Gnu.cpp:1990