clang  6.0.0svn
Fuchsia.cpp
Go to the documentation of this file.
1 //===--- Fuchsia.cpp - Fuchsia ToolChain Implementations --------*- 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 "Fuchsia.h"
11 #include "CommonArgs.h"
12 #include "clang/Config/config.h"
14 #include "clang/Driver/Driver.h"
16 #include "clang/Driver/Options.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Support/Path.h"
20 
21 using namespace clang::driver;
22 using namespace clang::driver::toolchains;
23 using namespace clang::driver::tools;
24 using namespace clang;
25 using namespace llvm::opt;
26 
27 void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA,
28  const InputInfo &Output,
29  const InputInfoList &Inputs,
30  const ArgList &Args,
31  const char *LinkingOutput) const {
33  static_cast<const toolchains::Fuchsia &>(getToolChain());
34  const Driver &D = ToolChain.getDriver();
35 
36  ArgStringList CmdArgs;
37 
38  // Silence warning for "clang -g foo.o -o foo"
39  Args.ClaimAllArgs(options::OPT_g_Group);
40  // and "clang -emit-llvm foo.o -o foo"
41  Args.ClaimAllArgs(options::OPT_emit_llvm);
42  // and for "clang -w foo.o -o foo". Other warning options are already
43  // handled somewhere else.
44  Args.ClaimAllArgs(options::OPT_w);
45 
46  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
47  if (llvm::sys::path::filename(Exec).equals_lower("ld.lld") ||
48  llvm::sys::path::stem(Exec).equals_lower("ld.lld")) {
49  CmdArgs.push_back("-z");
50  CmdArgs.push_back("rodynamic");
51  }
52 
53  if (!D.SysRoot.empty())
54  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
55 
56  if (!Args.hasArg(options::OPT_shared) && !Args.hasArg(options::OPT_r))
57  CmdArgs.push_back("-pie");
58 
59  if (Args.hasArg(options::OPT_rdynamic))
60  CmdArgs.push_back("-export-dynamic");
61 
62  if (Args.hasArg(options::OPT_s))
63  CmdArgs.push_back("-s");
64 
65  if (Args.hasArg(options::OPT_r)) {
66  CmdArgs.push_back("-r");
67  } else {
68  CmdArgs.push_back("--build-id");
69  CmdArgs.push_back("--hash-style=gnu");
70  }
71 
72  CmdArgs.push_back("--eh-frame-hdr");
73 
74  if (Args.hasArg(options::OPT_static))
75  CmdArgs.push_back("-Bstatic");
76  else if (Args.hasArg(options::OPT_shared))
77  CmdArgs.push_back("-shared");
78 
79  if (!Args.hasArg(options::OPT_shared)) {
80  std::string Dyld = D.DyldPrefix;
81  if (ToolChain.getSanitizerArgs().needsAsanRt() &&
82  ToolChain.getSanitizerArgs().needsSharedRt())
83  Dyld += "asan/";
84  Dyld += "ld.so.1";
85  CmdArgs.push_back("-dynamic-linker");
86  CmdArgs.push_back(Args.MakeArgString(Dyld));
87  }
88 
89  CmdArgs.push_back("-o");
90  CmdArgs.push_back(Output.getFilename());
91 
92  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
93  if (!Args.hasArg(options::OPT_shared)) {
94  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("Scrt1.o")));
95  }
96  }
97 
98  Args.AddAllArgs(CmdArgs, options::OPT_L);
99  Args.AddAllArgs(CmdArgs, options::OPT_u);
100 
101  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
102 
103  addSanitizerRuntimes(ToolChain, Args, CmdArgs);
104 
105  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
106 
107  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
108  if (Args.hasArg(options::OPT_static))
109  CmdArgs.push_back("-Bdynamic");
110 
111  if (D.CCCIsCXX()) {
112  if (ToolChain.ShouldLinkCXXStdlib(Args))
113  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
114  CmdArgs.push_back("-lm");
115  }
116 
117  AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
118 
119  if (Args.hasArg(options::OPT_pthread) ||
120  Args.hasArg(options::OPT_pthreads))
121  CmdArgs.push_back("-lpthread");
122 
123  if (Args.hasArg(options::OPT_fsplit_stack))
124  CmdArgs.push_back("--wrap=pthread_create");
125 
126  CmdArgs.push_back("-lc");
127  }
128 
129  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
130 }
131 
132 /// Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly.
133 
134 static std::string normalizeTriple(llvm::Triple Triple) {
136  T += Triple.getArchName();
137  T += "-";
138  T += Triple.getOSName();
139  return T.str();
140 }
141 
142 static std::string getTargetDir(const Driver &D,
143  llvm::Triple Triple) {
144  SmallString<128> P(llvm::sys::path::parent_path(D.Dir));
145  llvm::sys::path::append(P, "lib", normalizeTriple(Triple));
146  return P.str();
147 }
148 
149 Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple,
150  const ArgList &Args)
151  : ToolChain(D, Triple, Args) {
152  getProgramPaths().push_back(getDriver().getInstalledDir());
153  if (getDriver().getInstalledDir() != D.Dir)
154  getProgramPaths().push_back(D.Dir);
155 
157  llvm::sys::path::append(P, "lib");
158  getFilePaths().push_back(P.str());
159 
160  if (!D.SysRoot.empty()) {
162  llvm::sys::path::append(P, "lib");
163  getFilePaths().push_back(P.str());
164  }
165 }
166 
167 std::string Fuchsia::ComputeEffectiveClangTriple(const ArgList &Args,
168  types::ID InputType) const {
169  llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
170  Triple.setTriple(normalizeTriple(Triple));
171  return Triple.getTriple();
172 }
173 
175  return new tools::fuchsia::Linker(*this);
176 }
177 
179  const ArgList &Args) const {
180  if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) {
181  StringRef Value = A->getValue();
182  if (Value != "compiler-rt")
183  getDriver().Diag(clang::diag::err_drv_invalid_rtlib_name)
184  << A->getAsString(Args);
185  }
186 
188 }
189 
191 Fuchsia::GetCXXStdlibType(const ArgList &Args) const {
192  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
193  StringRef Value = A->getValue();
194  if (Value != "libc++")
195  getDriver().Diag(diag::err_drv_invalid_stdlib_name)
196  << A->getAsString(Args);
197  }
198 
199  return ToolChain::CST_Libcxx;
200 }
201 
202 void Fuchsia::addClangTargetOptions(const ArgList &DriverArgs,
203  ArgStringList &CC1Args,
204  Action::OffloadKind) const {
205  if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
206  options::OPT_fno_use_init_array, true))
207  CC1Args.push_back("-fuse-init-array");
208 }
209 
210 void Fuchsia::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
211  ArgStringList &CC1Args) const {
212  const Driver &D = getDriver();
213 
214  if (DriverArgs.hasArg(options::OPT_nostdinc))
215  return;
216 
217  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
219  llvm::sys::path::append(P, "include");
220  addSystemInclude(DriverArgs, CC1Args, P);
221  }
222 
223  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
224  return;
225 
226  // Check for configure-time C include directories.
227  StringRef CIncludeDirs(C_INCLUDE_DIRS);
228  if (CIncludeDirs != "") {
230  CIncludeDirs.split(dirs, ":");
231  for (StringRef dir : dirs) {
232  StringRef Prefix =
233  llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
234  addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
235  }
236  return;
237  }
238 
239  if (!D.SysRoot.empty()) {
241  llvm::sys::path::append(P, "include");
242  addExternCSystemInclude(DriverArgs, CC1Args, P.str());
243  }
244 }
245 
246 void Fuchsia::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
247  ArgStringList &CC1Args) const {
248  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
249  DriverArgs.hasArg(options::OPT_nostdincxx))
250  return;
251 
252  switch (GetCXXStdlibType(DriverArgs)) {
253  case ToolChain::CST_Libcxx: {
255  llvm::sys::path::append(P, "include", "c++", "v1");
256  addSystemInclude(DriverArgs, CC1Args, P.str());
257  break;
258  }
259 
260  default:
261  llvm_unreachable("invalid stdlib name");
262  }
263 }
264 
265 void Fuchsia::AddCXXStdlibLibArgs(const ArgList &Args,
266  ArgStringList &CmdArgs) const {
267  switch (GetCXXStdlibType(Args)) {
269  CmdArgs.push_back("-lc++");
270  CmdArgs.push_back("-lc++abi");
271  CmdArgs.push_back("-lunwind");
272  break;
273 
275  llvm_unreachable("invalid stdlib name");
276  }
277 }
278 
281  Res |= SanitizerKind::SafeStack;
282  Res |= SanitizerKind::Address;
283  return Res;
284 }
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments...
Definition: ToolChain.cpp:679
virtual std::string ComputeLLVMTriple(const llvm::opt::ArgList &Args, types::ID InputType=types::TY_INVALID) const
ComputeLLVMTriple - Return the LLVM target triple to use, after taking command line arguments into ac...
Definition: ToolChain.cpp:482
StringRef P
std::string DyldPrefix
Dynamic loader prefix, if present.
Definition: Driver.h:152
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:116
void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
Definition: Fuchsia.cpp:246
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
path_list & getProgramPaths()
Definition: ToolChain.h:205
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:127
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:391
path_list & getFilePaths()
Definition: ToolChain.h:202
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
Tool * buildLinker() const override
Definition: Fuchsia.cpp:174
RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override
Definition: Fuchsia.cpp:178
const FunctionProtoType * T
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:189
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: Fuchsia.cpp:265
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Fuchsia.cpp:279
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
const Driver & getDriver() const
Definition: ToolChain.h:167
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: Fuchsia.cpp:210
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:174
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:664
static std::string getTargetDir(const Driver &D, llvm::Triple Triple)
Definition: Fuchsia.cpp:142
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.
uint64_t SanitizerMask
Definition: Sanitizers.h:24
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
std::string SysRoot
sysroot, if present
Definition: Driver.h:149
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
const SanitizerArgs & getSanitizerArgs() const
Definition: ToolChain.cpp:105
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:34
const llvm::Triple & getTriple() const
Definition: ToolChain.h:169
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:776
std::string ComputeEffectiveClangTriple(const llvm::opt::ArgList &Args, types::ID InputType) const override
ComputeEffectiveClangTriple - Return the Clang triple to use for this target, which may take into acc...
Definition: Fuchsia.cpp:167
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition: Fuchsia.cpp:202
static std::string normalizeTriple(llvm::Triple Triple)
Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly.
Definition: Fuchsia.cpp:134
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition: Fuchsia.cpp:191
Fuchsia(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Fuchsia.cpp:149
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:73
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:139