blob: f076b528ce4b8e8ff5b95ab1464c09feeda8e351 [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
Heejin Ahn9d5a0892019-02-11 22:47:50 +000023void parseThreadArgs(const Driver &Driver, const ArgList &DriverArgs,
24 bool &Pthread, StringRef &ThreadModel,
25 bool CheckForErrors = true) {
26 // Default value for -pthread / -mthread-model options, each being false /
27 // "single".
28 Pthread =
29 DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread, false);
30 ThreadModel =
31 DriverArgs.getLastArgValue(options::OPT_mthread_model, "single");
32 if (!CheckForErrors)
33 return;
34
35 // Did user explicitly specify -mthread-model / -pthread?
36 bool HasThreadModel = DriverArgs.hasArg(options::OPT_mthread_model);
37 bool HasPthread = Pthread && DriverArgs.hasArg(options::OPT_pthread);
38 // '-pthread' cannot be used with '-mthread-model single'
39 if (HasPthread && HasThreadModel && ThreadModel == "single")
40 Driver.Diag(diag::err_drv_argument_not_allowed_with)
41 << "-pthread" << "-mthread-model single";
42}
43
David L. Jonesf561aba2017-03-08 01:02:16 +000044wasm::Linker::Linker(const ToolChain &TC)
Heejin Ahn4fa8dd92018-08-31 20:57:00 +000045 : GnuTool("wasm::Linker", "lld", TC) {}
David L. Jonesf561aba2017-03-08 01:02:16 +000046
Dan Gohman055a6f02019-01-15 06:58:16 +000047/// Following the conventions in https://wiki.debian.org/Multiarch/Tuples,
48/// we remove the vendor field to form the multiarch triple.
49static std::string getMultiarchTriple(const Driver &D,
50 const llvm::Triple &TargetTriple,
51 StringRef SysRoot) {
52 return (TargetTriple.getArchName() + "-" +
53 TargetTriple.getOSAndEnvironmentName()).str();
54}
55
Heejin Ahn4fa8dd92018-08-31 20:57:00 +000056bool wasm::Linker::isLinkJob() const { return true; }
David L. Jonesf561aba2017-03-08 01:02:16 +000057
Heejin Ahn4fa8dd92018-08-31 20:57:00 +000058bool wasm::Linker::hasIntegratedCPP() const { return false; }
David L. Jonesf561aba2017-03-08 01:02:16 +000059
60void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
61 const InputInfo &Output,
62 const InputInfoList &Inputs,
63 const ArgList &Args,
64 const char *LinkingOutput) const {
65
66 const ToolChain &ToolChain = getToolChain();
David L. Jonesf561aba2017-03-08 01:02:16 +000067 const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath());
68 ArgStringList CmdArgs;
David L. Jonesf561aba2017-03-08 01:02:16 +000069
David L. Jonesf561aba2017-03-08 01:02:16 +000070 if (Args.hasArg(options::OPT_s))
71 CmdArgs.push_back("--strip-all");
David L. Jonesf561aba2017-03-08 01:02:16 +000072
73 Args.AddAllArgs(CmdArgs, options::OPT_L);
Sam Cleggd09a3562017-12-02 23:11:13 +000074 Args.AddAllArgs(CmdArgs, options::OPT_u);
David L. Jonesf561aba2017-03-08 01:02:16 +000075 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
76
Sam Clegg471d7af2017-10-27 18:10:19 +000077 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
78 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
79
David L. Jonesf561aba2017-03-08 01:02:16 +000080 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
81
82 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
Nico Weber0ee47d92017-07-25 18:02:57 +000083 if (ToolChain.ShouldLinkCXXStdlib(Args))
David L. Jonesf561aba2017-03-08 01:02:16 +000084 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
85
86 if (Args.hasArg(options::OPT_pthread))
87 CmdArgs.push_back("-lpthread");
88
89 CmdArgs.push_back("-lc");
Sam Clegga08631e2017-10-27 00:26:07 +000090 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
David L. Jonesf561aba2017-03-08 01:02:16 +000091 }
92
David L. Jonesf561aba2017-03-08 01:02:16 +000093 CmdArgs.push_back("-o");
94 CmdArgs.push_back(Output.getFilename());
95
96 C.addCommand(llvm::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
97}
98
99WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
100 const llvm::opt::ArgList &Args)
Heejin Ahn4fa8dd92018-08-31 20:57:00 +0000101 : ToolChain(D, Triple, Args) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000102
103 assert(Triple.isArch32Bit() != Triple.isArch64Bit());
Sam Clegg27ea1562017-05-09 17:47:50 +0000104
105 getProgramPaths().push_back(getDriver().getInstalledDir());
106
Dan Gohman055a6f02019-01-15 06:58:16 +0000107 if (getTriple().getOS() == llvm::Triple::UnknownOS) {
108 // Theoretically an "unknown" OS should mean no standard libraries, however
109 // it could also mean that a custom set of libraries is in use, so just add
110 // /lib to the search path. Disable multiarch in this case, to discourage
111 // paths containing "unknown" from acquiring meanings.
112 getFilePaths().push_back(getDriver().SysRoot + "/lib");
113 } else {
114 const std::string MultiarchTriple =
115 getMultiarchTriple(getDriver(), Triple, getDriver().SysRoot);
116 getFilePaths().push_back(getDriver().SysRoot + "/lib/" + MultiarchTriple);
117 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000118}
119
120bool WebAssembly::IsMathErrnoDefault() const { return false; }
121
122bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
123
124bool WebAssembly::UseObjCMixedDispatch() const { return true; }
125
126bool WebAssembly::isPICDefault() const { return false; }
127
128bool WebAssembly::isPIEDefault() const { return false; }
129
130bool WebAssembly::isPICDefaultForced() const { return false; }
131
132bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
133
David L. Jonesf561aba2017-03-08 01:02:16 +0000134bool WebAssembly::hasBlocksRuntime() const { return false; }
135
136// TODO: Support profiling.
137bool WebAssembly::SupportsProfiling() const { return false; }
138
139bool WebAssembly::HasNativeLLVMSupport() const { return true; }
140
141void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000142 ArgStringList &CC1Args,
143 Action::OffloadKind) const {
David L. Jonesf561aba2017-03-08 01:02:16 +0000144 if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
145 options::OPT_fno_use_init_array, true))
146 CC1Args.push_back("-fuse-init-array");
Heejin Ahn9d5a0892019-02-11 22:47:50 +0000147
148 // Either '-mthread-model posix' or '-pthread' sets '-target-feature
149 // +atomics'. We intentionally didn't create '-matomics' and set the atomics
150 // target feature here depending on the other two options.
151 bool Pthread = false;
152 StringRef ThreadModel = "";
153 parseThreadArgs(getDriver(), DriverArgs, Pthread, ThreadModel);
154 if (Pthread || ThreadModel != "single") {
155 CC1Args.push_back("-target-feature");
156 CC1Args.push_back("+atomics");
157 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000158}
159
160ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
161 return ToolChain::RLT_CompilerRT;
162}
163
Heejin Ahn4fa8dd92018-08-31 20:57:00 +0000164ToolChain::CXXStdlibType
165WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
Sam Cleggffbfc0f2018-01-12 17:54:49 +0000166 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
167 StringRef Value = A->getValue();
168 if (Value != "libc++")
169 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
170 << A->getAsString(Args);
171 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000172 return ToolChain::CST_Libcxx;
173}
174
175void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
176 ArgStringList &CC1Args) const {
Dan Gohman055a6f02019-01-15 06:58:16 +0000177 if (!DriverArgs.hasArg(options::OPT_nostdinc)) {
178 if (getTriple().getOS() != llvm::Triple::UnknownOS) {
179 const std::string MultiarchTriple =
180 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
181 addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include/" + MultiarchTriple);
182 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000183 addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include");
Dan Gohman055a6f02019-01-15 06:58:16 +0000184 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000185}
186
187void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
188 ArgStringList &CC1Args) const {
189 if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
Dan Gohman055a6f02019-01-15 06:58:16 +0000190 !DriverArgs.hasArg(options::OPT_nostdincxx)) {
191 if (getTriple().getOS() != llvm::Triple::UnknownOS) {
192 const std::string MultiarchTriple =
193 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
194 addSystemInclude(DriverArgs, CC1Args,
195 getDriver().SysRoot + "/include/" + MultiarchTriple + "/c++/v1");
196 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000197 addSystemInclude(DriverArgs, CC1Args,
198 getDriver().SysRoot + "/include/c++/v1");
Dan Gohman055a6f02019-01-15 06:58:16 +0000199 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000200}
201
Sam Cleggffbfc0f2018-01-12 17:54:49 +0000202void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
203 llvm::opt::ArgStringList &CmdArgs) const {
204
205 switch (GetCXXStdlibType(Args)) {
206 case ToolChain::CST_Libcxx:
207 CmdArgs.push_back("-lc++");
208 CmdArgs.push_back("-lc++abi");
209 break;
210 case ToolChain::CST_Libstdcxx:
211 llvm_unreachable("invalid stdlib name");
212 }
213}
214
Heejin Ahn9d5a0892019-02-11 22:47:50 +0000215std::string WebAssembly::getThreadModel(const ArgList &DriverArgs) const {
216 // The WebAssembly MVP does not yet support threads. We set this to "posix"
217 // when '-pthread' is set.
218 bool Pthread = false;
219 StringRef ThreadModel = "";
220 parseThreadArgs(getDriver(), DriverArgs, Pthread, ThreadModel, false);
221 if (Pthread)
222 return "posix";
223 return ThreadModel;
Dan Gohman1384ee92017-11-27 21:39:16 +0000224}
225
David L. Jonesf561aba2017-03-08 01:02:16 +0000226Tool *WebAssembly::buildLinker() const {
227 return new tools::wasm::Linker(*this);
228}