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