clang  6.0.0svn
WebAssembly.cpp
Go to the documentation of this file.
1 //===--- 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"
13 #include "clang/Driver/Driver.h"
14 #include "clang/Driver/Options.h"
15 #include "llvm/Option/ArgList.h"
16 
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang::driver::toolchains;
20 using namespace clang;
21 using namespace llvm::opt;
22 
24  : GnuTool("wasm::Linker", "lld", TC) {}
25 
27  return true;
28 }
29 
31  return false;
32 }
33 
35  const InputInfo &Output,
36  const InputInfoList &Inputs,
37  const ArgList &Args,
38  const char *LinkingOutput) const {
39 
40  const ToolChain &ToolChain = getToolChain();
41  const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath());
42  ArgStringList CmdArgs;
43  CmdArgs.push_back("-flavor");
44  CmdArgs.push_back("wasm");
45 
46  if (Args.hasArg(options::OPT_s))
47  CmdArgs.push_back("--strip-all");
48 
49  Args.AddAllArgs(CmdArgs, options::OPT_L);
50  Args.AddAllArgs(CmdArgs, options::OPT_u);
51  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
52 
53  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
54  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
55 
56  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
57 
58  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
59  if (ToolChain.ShouldLinkCXXStdlib(Args))
60  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
61 
62  if (Args.hasArg(options::OPT_pthread))
63  CmdArgs.push_back("-lpthread");
64 
65  CmdArgs.push_back("-allow-undefined-file");
66  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("wasm.syms")));
67  CmdArgs.push_back("-lc");
68  AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
69  }
70 
71  CmdArgs.push_back("-o");
72  CmdArgs.push_back(Output.getFilename());
73 
74  C.addCommand(llvm::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
75 }
76 
77 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
78  const llvm::opt::ArgList &Args)
79  : ToolChain(D, Triple, Args) {
80 
81  assert(Triple.isArch32Bit() != Triple.isArch64Bit());
82 
83  getProgramPaths().push_back(getDriver().getInstalledDir());
84 
85  getFilePaths().push_back(getDriver().SysRoot + "/lib");
86 }
87 
88 bool WebAssembly::IsMathErrnoDefault() const { return false; }
89 
90 bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
91 
92 bool WebAssembly::UseObjCMixedDispatch() const { return true; }
93 
94 bool WebAssembly::isPICDefault() const { return false; }
95 
96 bool WebAssembly::isPIEDefault() const { return false; }
97 
98 bool WebAssembly::isPICDefaultForced() const { return false; }
99 
100 bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
101 
102 bool WebAssembly::hasBlocksRuntime() const { return false; }
103 
104 // TODO: Support profiling.
105 bool WebAssembly::SupportsProfiling() const { return false; }
106 
107 bool WebAssembly::HasNativeLLVMSupport() const { return true; }
108 
109 void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
110  ArgStringList &CC1Args,
111  Action::OffloadKind) const {
112  if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
113  options::OPT_fno_use_init_array, true))
114  CC1Args.push_back("-fuse-init-array");
115 }
116 
117 ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
119 }
120 
121 ToolChain::CXXStdlibType WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
122  return ToolChain::CST_Libcxx;
123 }
124 
125 void 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 
131 void 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 
139 std::string WebAssembly::getThreadModel() const {
140  // The WebAssembly MVP does not yet support threads; for now, use the
141  // "single" threading model, which lowers atomics to non-atomic operations.
142  // When threading support is standardized and implemented in popular engines,
143  // this override should be removed.
144  return "single";
145 }
146 
147 Tool *WebAssembly::buildLinker() const {
148  return new tools::wasm::Linker(*this);
149 }
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:399
const char * getFilename() const
Definition: InputInfo.h:84
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:391
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:189
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:740
Base class for all GNU tools that provide the same behavior when it comes to response files support...
Definition: Gnu.h:41
const Driver & getDriver() const
Definition: ToolChain.h:167
bool isLinkJob() const override
Definition: WebAssembly.cpp:26
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:717
Dataflow Directional Tag Classes.
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
bool hasIntegratedCPP() const override
Definition: WebAssembly.cpp:30
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:34
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:34
const ToolChain & getToolChain() const
Definition: Tool.h:84
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:723
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:73