clang  5.0.0
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 Driver &D = ToolChain.getDriver();
42  const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath());
43  ArgStringList CmdArgs;
44  CmdArgs.push_back("-flavor");
45  CmdArgs.push_back("wasm");
46 
47  // Enable garbage collection of unused input sections by default, since code
48  // size is of particular importance. This is significantly facilitated by
49  // the enabling of -ffunction-sections and -fdata-sections in
50  // Clang::ConstructJob.
51  if (areOptimizationsEnabled(Args))
52  CmdArgs.push_back("--gc-sections");
53 
54  if (Args.hasArg(options::OPT_rdynamic))
55  CmdArgs.push_back("-export-dynamic");
56  if (Args.hasArg(options::OPT_s))
57  CmdArgs.push_back("--strip-all");
58  if (Args.hasArg(options::OPT_shared))
59  CmdArgs.push_back("-shared");
60  if (Args.hasArg(options::OPT_static))
61  CmdArgs.push_back("-Bstatic");
62 
63  Args.AddAllArgs(CmdArgs, options::OPT_L);
64  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
65 
66  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
67  if (Args.hasArg(options::OPT_shared))
68  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("rcrt1.o")));
69  else if (Args.hasArg(options::OPT_pie))
70  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("Scrt1.o")));
71  else
72  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
73 
74  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
75  }
76 
77  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
78 
79  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
80  if (D.CCCIsCXX())
81  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
82 
83  if (Args.hasArg(options::OPT_pthread))
84  CmdArgs.push_back("-lpthread");
85 
86  CmdArgs.push_back("-allow-undefined-file");
87  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("wasm.syms")));
88  CmdArgs.push_back("-lc");
89  CmdArgs.push_back("-lcompiler_rt");
90  }
91 
92  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
93  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
94 
95  CmdArgs.push_back("-o");
96  CmdArgs.push_back(Output.getFilename());
97 
98  C.addCommand(llvm::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
99 }
100 
101 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
102  const llvm::opt::ArgList &Args)
103  : ToolChain(D, Triple, Args) {
104 
105  assert(Triple.isArch32Bit() != Triple.isArch64Bit());
106 
107  getProgramPaths().push_back(getDriver().getInstalledDir());
108 
109  getFilePaths().push_back(getDriver().SysRoot + "/lib");
110 }
111 
112 bool WebAssembly::IsMathErrnoDefault() const { return false; }
113 
114 bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
115 
116 bool WebAssembly::UseObjCMixedDispatch() const { return true; }
117 
118 bool WebAssembly::isPICDefault() const { return false; }
119 
120 bool WebAssembly::isPIEDefault() const { return false; }
121 
122 bool WebAssembly::isPICDefaultForced() const { return false; }
123 
124 bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
125 
126 // TODO: Support Objective C stuff.
127 bool WebAssembly::SupportsObjCGC() const { return false; }
128 
129 bool WebAssembly::hasBlocksRuntime() const { return false; }
130 
131 // TODO: Support profiling.
132 bool WebAssembly::SupportsProfiling() const { return false; }
133 
134 bool WebAssembly::HasNativeLLVMSupport() const { return true; }
135 
136 void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
137  ArgStringList &CC1Args,
138  Action::OffloadKind) const {
139  if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
140  options::OPT_fno_use_init_array, true))
141  CC1Args.push_back("-fuse-init-array");
142 }
143 
144 ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
146 }
147 
148 ToolChain::CXXStdlibType WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
149  return ToolChain::CST_Libcxx;
150 }
151 
152 void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
153  ArgStringList &CC1Args) const {
154  if (!DriverArgs.hasArg(options::OPT_nostdinc))
155  addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include");
156 }
157 
158 void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
159  ArgStringList &CC1Args) const {
160  if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
161  !DriverArgs.hasArg(options::OPT_nostdincxx))
162  addSystemInclude(DriverArgs, CC1Args,
163  getDriver().SysRoot + "/include/c++/v1");
164 }
165 
166 Tool *WebAssembly::buildLinker() const {
167  return new tools::wasm::Linker(*this);
168 }
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:174
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
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:651
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
const Driver & getDriver() const
Definition: ToolChain.h:142
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:189
const char * getFilename() const
Definition: InputInfo.h:84
Base class for all GNU tools that provide the same behavior when it comes to response files support...
Definition: Gnu.h:41
bool isLinkJob() const override
Definition: WebAssembly.cpp:26
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
bool areOptimizationsEnabled(const llvm::opt::ArgList &Args)
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:34
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:666
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:375
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:367
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:50