blob: 94f7279bbdbaf95d1eccf6fa397292d3980f6773 [file] [log] [blame]
David L. Jonesf561aba2017-03-08 01:02:16 +00001//===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "WebAssembly.h"
11#include "CommonArgs.h"
12#include "clang/Driver/Compilation.h"
13#include "clang/Driver/Driver.h"
Sam Cleggffbfc0f2018-01-12 17:54:49 +000014#include "clang/Driver/DriverDiagnostic.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000015#include "clang/Driver/Options.h"
16#include "llvm/Option/ArgList.h"
17
18using namespace clang::driver;
19using namespace clang::driver::tools;
20using namespace clang::driver::toolchains;
21using namespace clang;
22using namespace llvm::opt;
23
24wasm::Linker::Linker(const ToolChain &TC)
25 : GnuTool("wasm::Linker", "lld", TC) {}
26
27bool wasm::Linker::isLinkJob() const {
28 return true;
29}
30
31bool wasm::Linker::hasIntegratedCPP() const {
32 return false;
33}
34
35void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
36 const InputInfo &Output,
37 const InputInfoList &Inputs,
38 const ArgList &Args,
39 const char *LinkingOutput) const {
40
41 const ToolChain &ToolChain = getToolChain();
David L. Jonesf561aba2017-03-08 01:02:16 +000042 const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath());
43 ArgStringList CmdArgs;
44 CmdArgs.push_back("-flavor");
Sam Clegg27ea1562017-05-09 17:47:50 +000045 CmdArgs.push_back("wasm");
David L. Jonesf561aba2017-03-08 01:02:16 +000046
David L. Jonesf561aba2017-03-08 01:02:16 +000047 if (Args.hasArg(options::OPT_s))
48 CmdArgs.push_back("--strip-all");
David L. Jonesf561aba2017-03-08 01:02:16 +000049
50 Args.AddAllArgs(CmdArgs, options::OPT_L);
Sam Cleggd09a3562017-12-02 23:11:13 +000051 Args.AddAllArgs(CmdArgs, options::OPT_u);
David L. Jonesf561aba2017-03-08 01:02:16 +000052 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
53
Sam Clegg471d7af2017-10-27 18:10:19 +000054 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
55 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
56
David L. Jonesf561aba2017-03-08 01:02:16 +000057 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
58
59 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
Nico Weber0ee47d92017-07-25 18:02:57 +000060 if (ToolChain.ShouldLinkCXXStdlib(Args))
David L. Jonesf561aba2017-03-08 01:02:16 +000061 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
62
63 if (Args.hasArg(options::OPT_pthread))
64 CmdArgs.push_back("-lpthread");
65
66 CmdArgs.push_back("-lc");
Sam Clegga08631e2017-10-27 00:26:07 +000067 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
David L. Jonesf561aba2017-03-08 01:02:16 +000068 }
69
David L. Jonesf561aba2017-03-08 01:02:16 +000070 CmdArgs.push_back("-o");
71 CmdArgs.push_back(Output.getFilename());
72
73 C.addCommand(llvm::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
74}
75
76WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
77 const llvm::opt::ArgList &Args)
78 : ToolChain(D, Triple, Args) {
79
80 assert(Triple.isArch32Bit() != Triple.isArch64Bit());
Sam Clegg27ea1562017-05-09 17:47:50 +000081
82 getProgramPaths().push_back(getDriver().getInstalledDir());
83
Sam Clegg8202e862017-06-23 00:02:55 +000084 getFilePaths().push_back(getDriver().SysRoot + "/lib");
David L. Jonesf561aba2017-03-08 01:02:16 +000085}
86
87bool WebAssembly::IsMathErrnoDefault() const { return false; }
88
89bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
90
91bool WebAssembly::UseObjCMixedDispatch() const { return true; }
92
93bool WebAssembly::isPICDefault() const { return false; }
94
95bool WebAssembly::isPIEDefault() const { return false; }
96
97bool WebAssembly::isPICDefaultForced() const { return false; }
98
99bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
100
David L. Jonesf561aba2017-03-08 01:02:16 +0000101bool WebAssembly::hasBlocksRuntime() const { return false; }
102
103// TODO: Support profiling.
104bool WebAssembly::SupportsProfiling() const { return false; }
105
106bool WebAssembly::HasNativeLLVMSupport() const { return true; }
107
108void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000109 ArgStringList &CC1Args,
110 Action::OffloadKind) const {
David L. Jonesf561aba2017-03-08 01:02:16 +0000111 if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
112 options::OPT_fno_use_init_array, true))
113 CC1Args.push_back("-fuse-init-array");
114}
115
116ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
117 return ToolChain::RLT_CompilerRT;
118}
119
120ToolChain::CXXStdlibType WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
Sam Cleggffbfc0f2018-01-12 17:54:49 +0000121 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
122 StringRef Value = A->getValue();
123 if (Value != "libc++")
124 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
125 << A->getAsString(Args);
126 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000127 return ToolChain::CST_Libcxx;
128}
129
130void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
131 ArgStringList &CC1Args) const {
132 if (!DriverArgs.hasArg(options::OPT_nostdinc))
133 addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include");
134}
135
136void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
137 ArgStringList &CC1Args) const {
138 if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
139 !DriverArgs.hasArg(options::OPT_nostdincxx))
140 addSystemInclude(DriverArgs, CC1Args,
141 getDriver().SysRoot + "/include/c++/v1");
142}
143
Sam Cleggffbfc0f2018-01-12 17:54:49 +0000144void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
145 llvm::opt::ArgStringList &CmdArgs) const {
146
147 switch (GetCXXStdlibType(Args)) {
148 case ToolChain::CST_Libcxx:
149 CmdArgs.push_back("-lc++");
150 CmdArgs.push_back("-lc++abi");
151 break;
152 case ToolChain::CST_Libstdcxx:
153 llvm_unreachable("invalid stdlib name");
154 }
155}
156
Dan Gohman1384ee92017-11-27 21:39:16 +0000157std::string WebAssembly::getThreadModel() const {
158 // The WebAssembly MVP does not yet support threads; for now, use the
159 // "single" threading model, which lowers atomics to non-atomic operations.
160 // When threading support is standardized and implemented in popular engines,
161 // this override should be removed.
162 return "single";
163}
164
David L. Jonesf561aba2017-03-08 01:02:16 +0000165Tool *WebAssembly::buildLinker() const {
166 return new tools::wasm::Linker(*this);
167}