blob: b89e043d88dab43eb20aa673f5ab05cf295b9fe5 [file] [log] [blame]
David L. Jonesf561aba2017-03-08 01:02:16 +00001//===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David L. Jonesf561aba2017-03-08 01:02:16 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "WebAssembly.h"
10#include "CommonArgs.h"
11#include "clang/Driver/Compilation.h"
12#include "clang/Driver/Driver.h"
Sam Cleggffbfc0f2018-01-12 17:54:49 +000013#include "clang/Driver/DriverDiagnostic.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000014#include "clang/Driver/Options.h"
15#include "llvm/Option/ArgList.h"
16
17using namespace clang::driver;
18using namespace clang::driver::tools;
19using namespace clang::driver::toolchains;
20using namespace clang;
21using namespace llvm::opt;
22
23wasm::Linker::Linker(const ToolChain &TC)
Heejin Ahn4fa8dd92018-08-31 20:57:00 +000024 : GnuTool("wasm::Linker", "lld", TC) {}
David L. Jonesf561aba2017-03-08 01:02:16 +000025
Dan Gohman055a6f02019-01-15 06:58:16 +000026/// Following the conventions in https://wiki.debian.org/Multiarch/Tuples,
27/// we remove the vendor field to form the multiarch triple.
28static std::string getMultiarchTriple(const Driver &D,
29 const llvm::Triple &TargetTriple,
30 StringRef SysRoot) {
31 return (TargetTriple.getArchName() + "-" +
32 TargetTriple.getOSAndEnvironmentName()).str();
33}
34
Heejin Ahn4fa8dd92018-08-31 20:57:00 +000035bool wasm::Linker::isLinkJob() const { return true; }
David L. Jonesf561aba2017-03-08 01:02:16 +000036
Heejin Ahn4fa8dd92018-08-31 20:57:00 +000037bool wasm::Linker::hasIntegratedCPP() const { return false; }
David L. Jonesf561aba2017-03-08 01:02:16 +000038
39void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
40 const InputInfo &Output,
41 const InputInfoList &Inputs,
42 const ArgList &Args,
43 const char *LinkingOutput) const {
44
45 const ToolChain &ToolChain = getToolChain();
David L. Jonesf561aba2017-03-08 01:02:16 +000046 const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath());
47 ArgStringList CmdArgs;
David L. Jonesf561aba2017-03-08 01:02:16 +000048
David L. Jonesf561aba2017-03-08 01:02:16 +000049 if (Args.hasArg(options::OPT_s))
50 CmdArgs.push_back("--strip-all");
David L. Jonesf561aba2017-03-08 01:02:16 +000051
52 Args.AddAllArgs(CmdArgs, options::OPT_L);
Sam Cleggd09a3562017-12-02 23:11:13 +000053 Args.AddAllArgs(CmdArgs, options::OPT_u);
David L. Jonesf561aba2017-03-08 01:02:16 +000054 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
55
Sam Clegg471d7af2017-10-27 18:10:19 +000056 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
57 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
58
David L. Jonesf561aba2017-03-08 01:02:16 +000059 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
60
61 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
Nico Weber0ee47d92017-07-25 18:02:57 +000062 if (ToolChain.ShouldLinkCXXStdlib(Args))
David L. Jonesf561aba2017-03-08 01:02:16 +000063 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
64
65 if (Args.hasArg(options::OPT_pthread))
66 CmdArgs.push_back("-lpthread");
67
68 CmdArgs.push_back("-lc");
Sam Clegga08631e2017-10-27 00:26:07 +000069 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
David L. Jonesf561aba2017-03-08 01:02:16 +000070 }
71
David L. Jonesf561aba2017-03-08 01:02:16 +000072 CmdArgs.push_back("-o");
73 CmdArgs.push_back(Output.getFilename());
74
75 C.addCommand(llvm::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
76}
77
78WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
79 const llvm::opt::ArgList &Args)
Heejin Ahn4fa8dd92018-08-31 20:57:00 +000080 : ToolChain(D, Triple, Args) {
David L. Jonesf561aba2017-03-08 01:02:16 +000081
82 assert(Triple.isArch32Bit() != Triple.isArch64Bit());
Sam Clegg27ea1562017-05-09 17:47:50 +000083
84 getProgramPaths().push_back(getDriver().getInstalledDir());
85
Dan Gohman055a6f02019-01-15 06:58:16 +000086 if (getTriple().getOS() == llvm::Triple::UnknownOS) {
87 // Theoretically an "unknown" OS should mean no standard libraries, however
88 // it could also mean that a custom set of libraries is in use, so just add
89 // /lib to the search path. Disable multiarch in this case, to discourage
90 // paths containing "unknown" from acquiring meanings.
91 getFilePaths().push_back(getDriver().SysRoot + "/lib");
92 } else {
93 const std::string MultiarchTriple =
94 getMultiarchTriple(getDriver(), Triple, getDriver().SysRoot);
95 getFilePaths().push_back(getDriver().SysRoot + "/lib/" + MultiarchTriple);
96 }
David L. Jonesf561aba2017-03-08 01:02:16 +000097}
98
99bool WebAssembly::IsMathErrnoDefault() const { return false; }
100
101bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
102
103bool WebAssembly::UseObjCMixedDispatch() const { return true; }
104
105bool WebAssembly::isPICDefault() const { return false; }
106
107bool WebAssembly::isPIEDefault() const { return false; }
108
109bool WebAssembly::isPICDefaultForced() const { return false; }
110
111bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
112
David L. Jonesf561aba2017-03-08 01:02:16 +0000113bool WebAssembly::hasBlocksRuntime() const { return false; }
114
115// TODO: Support profiling.
116bool WebAssembly::SupportsProfiling() const { return false; }
117
118bool WebAssembly::HasNativeLLVMSupport() const { return true; }
119
120void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000121 ArgStringList &CC1Args,
122 Action::OffloadKind) const {
David L. Jonesf561aba2017-03-08 01:02:16 +0000123 if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
124 options::OPT_fno_use_init_array, true))
125 CC1Args.push_back("-fuse-init-array");
126}
127
128ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
129 return ToolChain::RLT_CompilerRT;
130}
131
Heejin Ahn4fa8dd92018-08-31 20:57:00 +0000132ToolChain::CXXStdlibType
133WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
Sam Cleggffbfc0f2018-01-12 17:54:49 +0000134 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
135 StringRef Value = A->getValue();
136 if (Value != "libc++")
137 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
138 << A->getAsString(Args);
139 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000140 return ToolChain::CST_Libcxx;
141}
142
143void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
144 ArgStringList &CC1Args) const {
Dan Gohman055a6f02019-01-15 06:58:16 +0000145 if (!DriverArgs.hasArg(options::OPT_nostdinc)) {
146 if (getTriple().getOS() != llvm::Triple::UnknownOS) {
147 const std::string MultiarchTriple =
148 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
149 addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include/" + MultiarchTriple);
150 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000151 addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include");
Dan Gohman055a6f02019-01-15 06:58:16 +0000152 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000153}
154
155void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
156 ArgStringList &CC1Args) const {
157 if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
Dan Gohman055a6f02019-01-15 06:58:16 +0000158 !DriverArgs.hasArg(options::OPT_nostdincxx)) {
159 if (getTriple().getOS() != llvm::Triple::UnknownOS) {
160 const std::string MultiarchTriple =
161 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
162 addSystemInclude(DriverArgs, CC1Args,
163 getDriver().SysRoot + "/include/" + MultiarchTriple + "/c++/v1");
164 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000165 addSystemInclude(DriverArgs, CC1Args,
166 getDriver().SysRoot + "/include/c++/v1");
Dan Gohman055a6f02019-01-15 06:58:16 +0000167 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000168}
169
Sam Cleggffbfc0f2018-01-12 17:54:49 +0000170void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
171 llvm::opt::ArgStringList &CmdArgs) const {
172
173 switch (GetCXXStdlibType(Args)) {
174 case ToolChain::CST_Libcxx:
175 CmdArgs.push_back("-lc++");
176 CmdArgs.push_back("-lc++abi");
177 break;
178 case ToolChain::CST_Libstdcxx:
179 llvm_unreachable("invalid stdlib name");
180 }
181}
182
Dan Gohman1384ee92017-11-27 21:39:16 +0000183std::string WebAssembly::getThreadModel() const {
184 // The WebAssembly MVP does not yet support threads; for now, use the
185 // "single" threading model, which lowers atomics to non-atomic operations.
186 // When threading support is standardized and implemented in popular engines,
187 // this override should be removed.
188 return "single";
189}
190
David L. Jonesf561aba2017-03-08 01:02:16 +0000191Tool *WebAssembly::buildLinker() const {
192 return new tools::wasm::Linker(*this);
193}