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