blob: 6776a41dad06747995b30beafd7ad2a7f46fde7e [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)
Heejin Ahn4fa8dd92018-08-31 20:57:00 +000025 : GnuTool("wasm::Linker", "lld", TC) {}
David L. Jonesf561aba2017-03-08 01:02:16 +000026
Heejin Ahn4fa8dd92018-08-31 20:57:00 +000027bool wasm::Linker::isLinkJob() const { return true; }
David L. Jonesf561aba2017-03-08 01:02:16 +000028
Heejin Ahn4fa8dd92018-08-31 20:57:00 +000029bool wasm::Linker::hasIntegratedCPP() const { return false; }
David L. Jonesf561aba2017-03-08 01:02:16 +000030
31void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
32 const InputInfo &Output,
33 const InputInfoList &Inputs,
34 const ArgList &Args,
35 const char *LinkingOutput) const {
36
37 const ToolChain &ToolChain = getToolChain();
David L. Jonesf561aba2017-03-08 01:02:16 +000038 const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath());
39 ArgStringList CmdArgs;
David L. Jonesf561aba2017-03-08 01:02:16 +000040
David L. Jonesf561aba2017-03-08 01:02:16 +000041 if (Args.hasArg(options::OPT_s))
42 CmdArgs.push_back("--strip-all");
David L. Jonesf561aba2017-03-08 01:02:16 +000043
44 Args.AddAllArgs(CmdArgs, options::OPT_L);
Sam Cleggd09a3562017-12-02 23:11:13 +000045 Args.AddAllArgs(CmdArgs, options::OPT_u);
David L. Jonesf561aba2017-03-08 01:02:16 +000046 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
47
Sam Clegg471d7af2017-10-27 18:10:19 +000048 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
49 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
50
David L. Jonesf561aba2017-03-08 01:02:16 +000051 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
52
53 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
Nico Weber0ee47d92017-07-25 18:02:57 +000054 if (ToolChain.ShouldLinkCXXStdlib(Args))
David L. Jonesf561aba2017-03-08 01:02:16 +000055 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
56
57 if (Args.hasArg(options::OPT_pthread))
58 CmdArgs.push_back("-lpthread");
59
60 CmdArgs.push_back("-lc");
Sam Clegga08631e2017-10-27 00:26:07 +000061 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
David L. Jonesf561aba2017-03-08 01:02:16 +000062 }
63
David L. Jonesf561aba2017-03-08 01:02:16 +000064 CmdArgs.push_back("-o");
65 CmdArgs.push_back(Output.getFilename());
66
67 C.addCommand(llvm::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
68}
69
70WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
71 const llvm::opt::ArgList &Args)
Heejin Ahn4fa8dd92018-08-31 20:57:00 +000072 : ToolChain(D, Triple, Args) {
David L. Jonesf561aba2017-03-08 01:02:16 +000073
74 assert(Triple.isArch32Bit() != Triple.isArch64Bit());
Sam Clegg27ea1562017-05-09 17:47:50 +000075
76 getProgramPaths().push_back(getDriver().getInstalledDir());
77
Sam Clegg8202e862017-06-23 00:02:55 +000078 getFilePaths().push_back(getDriver().SysRoot + "/lib");
David L. Jonesf561aba2017-03-08 01:02:16 +000079}
80
81bool WebAssembly::IsMathErrnoDefault() const { return false; }
82
83bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
84
85bool WebAssembly::UseObjCMixedDispatch() const { return true; }
86
87bool WebAssembly::isPICDefault() const { return false; }
88
89bool WebAssembly::isPIEDefault() const { return false; }
90
91bool WebAssembly::isPICDefaultForced() const { return false; }
92
93bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
94
David L. Jonesf561aba2017-03-08 01:02:16 +000095bool WebAssembly::hasBlocksRuntime() const { return false; }
96
97// TODO: Support profiling.
98bool WebAssembly::SupportsProfiling() const { return false; }
99
100bool WebAssembly::HasNativeLLVMSupport() const { return true; }
101
102void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000103 ArgStringList &CC1Args,
104 Action::OffloadKind) const {
David L. Jonesf561aba2017-03-08 01:02:16 +0000105 if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
106 options::OPT_fno_use_init_array, true))
107 CC1Args.push_back("-fuse-init-array");
108}
109
110ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
111 return ToolChain::RLT_CompilerRT;
112}
113
Heejin Ahn4fa8dd92018-08-31 20:57:00 +0000114ToolChain::CXXStdlibType
115WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
Sam Cleggffbfc0f2018-01-12 17:54:49 +0000116 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
117 StringRef Value = A->getValue();
118 if (Value != "libc++")
119 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
120 << A->getAsString(Args);
121 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000122 return ToolChain::CST_Libcxx;
123}
124
125void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
126 ArgStringList &CC1Args) const {
127 if (!DriverArgs.hasArg(options::OPT_nostdinc))
128 addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include");
129}
130
131void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
132 ArgStringList &CC1Args) const {
133 if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
134 !DriverArgs.hasArg(options::OPT_nostdincxx))
135 addSystemInclude(DriverArgs, CC1Args,
136 getDriver().SysRoot + "/include/c++/v1");
137}
138
Sam Cleggffbfc0f2018-01-12 17:54:49 +0000139void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
140 llvm::opt::ArgStringList &CmdArgs) const {
141
142 switch (GetCXXStdlibType(Args)) {
143 case ToolChain::CST_Libcxx:
144 CmdArgs.push_back("-lc++");
145 CmdArgs.push_back("-lc++abi");
146 break;
147 case ToolChain::CST_Libstdcxx:
148 llvm_unreachable("invalid stdlib name");
149 }
150}
151
Dan Gohman1384ee92017-11-27 21:39:16 +0000152std::string WebAssembly::getThreadModel() const {
153 // The WebAssembly MVP does not yet support threads; for now, use the
154 // "single" threading model, which lowers atomics to non-atomic operations.
155 // When threading support is standardized and implemented in popular engines,
156 // this override should be removed.
157 return "single";
158}
159
David L. Jonesf561aba2017-03-08 01:02:16 +0000160Tool *WebAssembly::buildLinker() const {
161 return new tools::wasm::Linker(*this);
162}